【发布时间】:2018-04-03 05:18:08
【问题描述】:
试图从用户那里获取输入。用户无法计算输入的长度。所以我正在使用 ArrayList 添加来自扫描仪的输入。之后,我通常会尝试比较原始整数的 ArrayList 的内容。使用 get(int index) 返回错误:
package BubbleSort;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class BubbleSort {
public static ArrayList swap(ArrayList x, int i, int j) {
Collections.swap(x, i, j);
return x;
}
public static void main(String args[]) {
List a = new ArrayList();
Scanner sc = new Scanner(System.in);
a.add(sc.nextInt());
int x = a.size() - 1;
boolean unsorted = true;
while (unsorted) {
unsorted = false;
for (int i = 0; i < x; i++) {
if (a.get(i) > a.get(i + 1)) { // <===== Error
BubbleSort.swap(a, i, i + 1);
unsorted = true;
}
}
x--; // Space utilization
}
System.out.println(Arrays.toString(a));
}
}
错误:
The operator > is undefined for the argument type(s) java.lang.Object, java.lang.Object
如何像在数组中一样比较整数?
【问题讨论】:
-
您不能对对象使用关系运算符(如
>)。 -
我没有意识到它返回对象只是我发现了这个错误。
标签: java algorithm data-structures bubble-sort