这一行 result.add(temp.get(temp.size() - 3)); 在ArrayList是threeConsecutiveInt方法的本地时显示错误;注意我已经确定只显示错误,并且它不是语法,也不是定义失败。如果我把它移回去,温度也会。我可以添加到列表
public static void main(String[] args) {
// TODO code application logic here
ArrayList<Integer> colInts = new ArrayList<>();
ArrayList<Integer> temp = new ArrayList<>();
//ArrayList<Integer> result = new ArrayList<>();
colInts.add(54);
colInts.add(4);
colInts.add(544);
colInts.add(1237);
colInts.add(98757);
colInts.add(874);
colInts.add(54987);
colInts.add(874);
colInts.add(154987);
System.out.println(colInts);
//System.out.println(threeConsecutiveInt(colInts, temp, result));
System.out.println(threeConsecutiveInt(colInts, temp));
}
public static ArrayList<Integer> threeConsecutiveInt(ArrayList ints, ArrayList temp) { // , ArrayList result
ArrayList<Integer> result = new ArrayList<>();
int start = 0;
int count = ints.size() - 1;
int size = count;
for (int i = 0; i <= size; i++) {
temp.add(ints.get(i));
}
Collections.sort(temp);
result.clear();
result.add(temp.get(temp.size() - 3));
//result.add(temp.get(temp.size() - 2));
//result.add(temp.get(temp.size() - 1));
for (int i = 0; i < size; i++) {
ints.remove(ints.size() - 1);
}
return result;
}
答案 0 :(得分:0)
由于您没有发布实际的错误消息,我只是在这里猜测,但我很确定问题是您的正式方法参数是原始泛型类型。由于result
是ArrayList<Integer>
,因此要求ArrayList<Integer>
调用的正式参数也必须result.add()
才能生效public static ArrayList<Integer> threeConsecutiveInt(
ArrayList ints,
ArrayList temp)
{
。从这个改变你的功能声明:
public static ArrayList<Integer> threeConsecutiveInt(
ArrayList<Integer> ints,
ArrayList<Integer> temp)
{
到此:
{{1}}
答案 1 :(得分:0)
threeConsecutiveInt的temp参数声明为ArrayList(没有类型规范),因此其get()方法返回Object类型,但result只接受Integer实例。
将方法声明更改为:
public static ArrayList<Integer> threeConsecutiveInt(ArrayList<Integer> ints, ArrayList<Integer> temp) { // , ArrayList result
ArrayList<Integer> result = new ArrayList<>();