gpt4 book ai didi

java - Arraylist 索引越界

转载 作者:行者123 更新时间:2023-12-01 19:34:15 25 4
gpt4 key购买 nike

作业要求读取两个文件,并将学生的信息分别输入到两个ArrayList中。 s。然后我需要执行两个功能:组合两个列表并对组合列表进行排序。

public abstract class Tools {
public static ArrayList<JUPASStudent> readJUPASFile(String f) throws Exception {
ArrayList<JUPASStudent> jList = new ArrayList<JUPASStudent>();

BufferedReader readbuffer = null;
readbuffer = new BufferedReader(new FileReader(f));
String strRead;
while ((strRead = readbuffer.readLine()) != null) {
String splitarray[] = strRead.split("/t");
String firstentry = splitarray[0];
String secondentry = splitarray[1];
JUPASStudent x = new JUPASStudent(firstentry, Double.parseDouble(secondentry));
jList.add(x);
}

readbuffer.close();
return jList;
}

public static ArrayList<NonJUPASStudent> readNonJUPASFile(String f) throws Exception {
ArrayList<NonJUPASStudent> njList = new ArrayList<NonJUPASStudent>();

BufferedReader readbuffer = null;
readbuffer = new BufferedReader(new FileReader(f));
String strRead;
while ((strRead = readbuffer.readLine()) != null) {
String splitarray[] = strRead.split("/t");
String firstentry = splitarray[0];
String secondentry = splitarray[1];
NonJUPASStudent x = new NonJUPASStudent(firstentry, Double.parseDouble(secondentry));
njList.add(x);
}

readbuffer.close();
return njList;
}

public static ArrayList<Student> combineArrayList(ArrayList<JUPASStudent> S1, ArrayList<NonJUPASStudent> S2) {
ArrayList<Student> sList = new ArrayList<Student>();

for (int i = 0; i < S1.size(); i++)
sList.add(S1.get(i));
for (int i = 0; i < S2.size(); i++)
sList.add(S2.get(i));

return sList;
}

public static ArrayList<Student> sort(ArrayList<Student> s){
for (int i = 0; i < s.size()-1; i++) {
for (int j = 0; i < s.size()-i-1; j++) {
if (s.get(j).getResult() > s.get(j+1).getResult()) {
Student Temp = s.get(j);
s.set(j, s.get(j+1));
s.set(j+1, Temp);
}
}
}
return s;
}
}

但是,我不断收到“索引 1 超出长度 1 的范围”

最佳答案

在内部循环中,您定义了对错误计数器的约束。而不是:

for (int j = 0; i < s.size()-i-1; j++)

应该有:

for (int j = 0; j < s.size()-i-1; j++)

关于java - Arraylist 索引越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58425894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com