无法返回arrayList

时间:2015-11-05 11:26:24

标签: java arrays arraylist

出于某种原因,它说我的功能没有返回任何东西。但是我在写:

return (outputList);

...为了返回arrayList。我收到 缺少退货声明。

以下是我的其余代码

public static ArrayList<Integer> duplicatesRemoval(int [] inputList){
        ArrayList<Integer> outputList = new ArrayList<>();
        int length = inputList.length;
        int i = 0;
        while(i < length){
            int nextElement = inputList[i];
            int lengthOut = outputList.size();
            int j=0;
            boolean found = false;
            while (j <= lengthOut && found){
                int outputElement = outputList.get(j);
                if( nextElement == outputElement){
                    found = true;
                }
                j++;
            }
            if(!found){
                outputList.add(nextElement);
                i++;
        }
    return(outputList);      
}
}

3 个答案:

答案 0 :(得分:2)

你的return语句在while循环中,因此可能永远不会到达(如果inputList是一个空数组)。

答案 1 :(得分:0)

你应该在你的while循环

中使用后返回你的ArrayList

答案 2 :(得分:0)

这不是您问题的答案,但您可以使用HashMap来避免重复:

public static ArrayList<Integer> duplicatesRemoval(int [] inputList){ 
  HashMap<Integer, Integer> mArray = new HashMap<>();
  int i = 0;
  while (i < inputList.length){
    mArray.put(inputList(i), inputList(i))
    i++;
  }
  return new ArrayList<Integer>(mArray.values())
}