返回与参数不同的类型

时间:2017-02-20 17:11:25

标签: java

这是hw,我真的很困惑如何让我的代码返回我希望它返回的内容。我试图返回具有给定索引值的String值。我认为我所要做的就是返回给定索引处的字符串值,但我得不到正确的答案。

    public void add(String candidate){
    if (candidate.equals(null)){
        throw new RuntimeException();
    }
    String[] contenders = new String[candidates.length+1];

    // copy the array manually because I'm restricted from ArrayLists
    for (int i = 0; i < candidates.length; i++){
        contenders[i] = this.candidates[i];
    }
    this.candidate = candidate;
    contenders[contenders.length-1] = this.candidate;
    this.candidates = new String [contenders.length];

将值添加到新构造的数组后,测试人员希望获得给定索引的字符串值

    public String get(int index){
    if (index < 0 || index > candidates.length) {
        throw new RuntimeException("Your argument was not within bounds.");
    }
    for (int i = index; i < candidate.length(); i++){
        candidate = candidates[index];
    }
    return candidate;

我一直在研究它,我终于能够让候选者停止指向null它给出了给定索引的错误值,例如我想要候选人的'X'[3]但我得到'Y' '因为这是候选人保留的最后一个值。我试过只返回候选[索引],但它告诉我该索引的值为空。当我通过调试器时,似乎我的原始数组没有被正确复制,但我不确定接下来应该尝试什么。提前谢谢。

这是我的构造函数:

public CandidateList(){
    candidates = new String[0];
}

public CandidateList(String[] candidates){
    this.candidates = new String[candidates.length];
    CandidateList candidateList = new CandidateList();

1 个答案:

答案 0 :(得分:1)

您的代码中可以改进很多,让我添加一些注释

    public void add(String candidate){
    //if candidate is actually null you are calling null.equals
    //which means this will always result in a NullPointerException
    //you can remove this if if you want
    if (candidate.equals(null)){
        throw new RuntimeException();
    }

    ...

    //think about what you are doing here, 
    //you are setting this.candidates to a new empty array 
    //(is big contenders.length, but still empty) 
    this.candidates = new String [contenders.length];

第二部分:

    public String get(int index){
    //you are missing an '=' in index >= candidates.length
    if (index < 0 || index > candidates.length) {
        throw new RuntimeException("Your argument was not within bounds.");
    }
    //this for loop is wrong, you are changing 'i' but never use it.. 
    //just return candidates[index] like you said before. 
    //It was probably null because of the error above
    for (int i = index; i < candidate.length(); i++){
        candidate = candidates[index];
    }
    return candidate;

关于RuntimeException(RE)的注释:如果你捕获NullPointerException(NPE)并抛出RE,你实际上正在丢失信息(因为NPE是一个更具体的错误而不是RE)。如果你想抓住/抛出至少一个重要的消息,如“候选人不能为空”

现在让我们分析构造函数:

public CandidateList(){
    candidates = new String[0];
}

public CandidateList(String[] candidates){

    // you are doing the same error as above here:
    // when you do this you create an EMPTY list of size candidates.lenght
    // correct code is this.candidates = candidates
    this.candidates = new String[candidates.length];

    // this is not necessary, constructors don't need to return anything, 
    //here you are just creating a new instance that will not be used anywhere
    CandidateList candidateList = new CandidateList();

构造函数创建对象,它们不返回数据。我建议你看一下这个问题Does a Java constructor return the Object reference?,一般来看一下有关构造函数的更多信息