我正在尝试对数组列表进行冒泡排序,但我一直收到错误bad operand types for binary operator '>'
我正在尝试按数字顺序对其进行排序,以获得下面列出的测试分数。我是java的新手,对于该怎么做我很困惑。我很确定整数存储在列表中。
我的代码是:
public static void sort(){
int k = 0;
boolean exchangeMade = true;
while((k < classroom.size() - 1) &&exchangeMade){
exchangeMade = false;
k++;
for (int j = 0; j < classroom.size() - k; j++)
if(classroom.get(j) > classroom.get(j+1)){
swap(j, j+1);
swap(classroom, j, j+1);
exchangeMade = true;
}
}
该程序的其余代码是
public class Test {
private static FileInputStream inFile;
private static InputStreamReader inReader;
private static BufferedReader reader;
private static List<Student> classroom =
new ArrayList<Student>(); // ArrayList to store the classroom.
public static void main(String args[]) throws IOException {
initFile();
getData();
System.out.print(classroom); //output of the complete class.
sort();
System.out.print(classroom); //output after sorting.
inFile.close();
}
// preparing the file for input
public static void initFile() throws IOException {
inFile =
new FileInputStream(
"C:\\Users\\clamanna\\Google Drive\\Senior\\AP COMP SCI\\!!VHSJava\\!!APCSDATA\\truefalse.txt");
inReader = new InputStreamReader(inFile);
reader = new BufferedReader(inReader);
}
// Separate the id from the answers and store the answers in an array.
public static void getData() throws IOException {
String line = reader.readLine(); //Seed
String[] answerkey = new String[10]; //Store the answer key from the first line of the txt file.
for (int i = 0;
i < answerkey.length;
i++) { // take that line and place each answer in an array.
answerkey[i] = line.substring(i, i + 1);
}
line = reader.readLine(); // read the following line of the txt file.
while (line != null) // Read and create a student for each line.
{
String[] answers = new String[10];
StringTokenizer strTkn = new StringTokenizer(line);
String id = strTkn.nextToken();
String answerline = strTkn.nextToken();
for (int i = 0; i < answers.length; i++) {
answers[i] = answerline.substring(i, i + 1);
}
Student stu = new Student(id, answers);
stu.grade(answerkey, answers);
classroom.add(stu);
line = reader.readLine(); //updating what is being read
}
}
}
答案 0 :(得分:6)
>
和<
等关系型运算符仅定义为int
和double
等数字基元类型。
您也可以将它们用于等同的盒装类型,例如Integer
和Double
,因为这些类型可以自动取消装入基元。
要比较对象,您应该使用Comparable.compareTo
方法:
classroom.get(j).compareTo(classroom.get(j+1)) > 1
请注意,这需要您的元素实现Comparable
界面 - Integer
,Double
等常见类型,以及String
之类的内容(实现Comparable
它与词典顺序一致)。
如果您的元素类没有实现Comparable
(并且您无法对其进行更改以实现它),则可以使用名为Comparator
的interface Comparable<T> {
int compare(T a, T b);
}
“外部”形式,这是一个这样的界面:
compare
您不添加到您想要比较的类的 - 您将此接口实现为单独的类,以便a < b
返回负数,零或正数如果您分别考虑a == b
,a > b
和comparator.compare(classroom.get(j), classroom.get(j + 1)) > 1
,请编号。
然后,您将使用:
comparator
其中Comparator<Student>
是实现Comparator<? super Student>
的类的实例。 (实际上,它可以是means= numpy.arange(10)
means=pd.DataFrame(means.reshape(5,2))
stds=numpy.arange(10,20)
stds=pd.DataFrame(sts.reshape(5,2))
samples={}
for i in means.columns:
col={}
for j in means.index:
col[j]=numpy.random.normal(means.ix[j,i],stds.ix[j,i],2)
samples[i]=col
print(pd.DataFrame(samples))
# 0 1
#0 [0.0760974520154, 3.29439282825] [11.1292510583, 0.318246201796]
#1 [-25.4518020981, 19.2176263823] [17.0826945017, 9.36179435872]
#2 [14.5402484325, 8.33808246538] [6.96459947914, 26.5552235093]
#3 [0.775891790613, -2.09168601369] [2.38723023677, 15.8099942902]
#4 [-0.828518484847, 45.4592922652] [26.8088977308, 16.0818556353]
,但现在不要担心太多了。