Arraylist索引超出范围

问题描述 投票:0回答:4

作业需要阅读两个文件,并将学生的信息输入两个单独的数组列表中。然后,我需要执行2个功能:合并两个列表并对合并的列表进行排序。

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”

java
4个回答
2
投票

在内部循环中,您正在定义对错误计数器的约束。代替:

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

应该有:

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

1
投票

在java.util包中,我们可以:

  • Collections.sort(List); //排序列表,例如:

Collections.sort(Arrays.asList(13, 4));

  • Collections.addAll(Collection c,T ... elements); //加入列表,例如:

Collections.addAll(Arrays.asList(13, 4), Arrays.asList(3,4,5));

因此您不再需要编写自己的函数。


0
投票

您能否添加一些system.out语句。它将帮助您查看错误所在。我怀疑文件中的数据。


0
投票

您面临的问题可能在下一行中。在方法readNonJUPASFile和readJUPASFile中]

String secondentry = splitarray[1];

您正在基于/ t拆分字符串。如果希望基于选项卡拆分它,则应输入\ t。如果您的行不包含/ t,则可能会收到ArrayIndexOutOfBoundsException。您应该在分配变量之前检查长度,例如

if (splitarray.length > 1) {
   String firstentry = splitarray[0];
   String secondentry = splitarray[1];
}
热门问题
推荐问题
最新问题