对不起,标题不好,但我不知道该怎么说简单问题。

我在一个类中有一个静态函数,该函数返回该类的数组。
例如

//returns CustomStack[]
CustomStack.createCustomStacks({{1,2},{3,4}})


然后,我有一个单独的类,希望将此静态函数用于其他目的。我遇到的问题是决定如何处理错误和异常。

因此,在另一个单独的类中给出此功能:

public boolean StackConfirmer (CustomStack[] myCustomStacks) {
    ....
}


如何更改此功能/ CustomStack.createCustomStacks的输出,以便我也可以处理错误?如果仍然不清楚,我可以根据需要进行澄清/编辑。抱歉,英语不是我的母语。像我使用null吗?我使用-1吗?

编辑:这是更详细的代码:

public class CustomStack {
    ...constructors and other irrelevant functions...

    public static CustomStack[] createCustomStacks (some_input) {
        if some_input is valid -> return CustomStack[] with Stacks inside
        if some_input isn't valid -> I'm not sure what to do.  Raise exception?
    }
}

public class ProgramThatUsesCustomStacks {
    ...
    private boolean StackConfirmer (CustomStack[] myCustomStacks) {
        // How should I be checking if there was a valid input in the other class?
        ....
}

最佳答案

我建议使用-1作为数组中的第一个元素(假设它永远不会有-1而不会出现错误)。这将是发生错误的标志,然后可以将阵列填充到特定长度以传达错误的性质。也就是说,如果数据不好,则数组由{-1,0}组成;如果数据过多,则数组由{-1,0,0}等组成。

10-04 14:42
查看更多