我正在尝试根据年级水平对我的arrylist进行排序。但我不知道为什么它什么都没打印出来,我检查了arraylist,显示它是空的。 Plz帮助我。我是现任AP CS学生。 "名称"是我从图书馆导入的列表,不用担心。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import javax.swing.JOptionPane;
public class lab {
public static ArrayList<String> method2(ArrayList<String> random) {
ArrayList<String> lastfirst = new ArrayList<String>();
for (int a = 0; a < random.size(); a = a + 2) {
String str = random.get(a);
int space = str.indexOf(" ");
String last = str.substring(space + 1);
String first = str.substring(0, space);
String combo = last + " " + first;
String str2 = random.get(a + 1);
String namegrade = combo + "," + str2 + ",";
lastfirst.add(namegrade);
}
ArrayList<String> prek = new ArrayList<String>();
ArrayList<String> jrprek = new ArrayList<String>();
ArrayList<String> k = new ArrayList<String>();
ArrayList<String> one = new ArrayList<String>();
ArrayList<String> two = new ArrayList<String>();
ArrayList<String> three = new ArrayList<String>();
ArrayList<String> four = new ArrayList<String>();
ArrayList<String> five = new ArrayList<String>();
ArrayList<String> six = new ArrayList<String>();
ArrayList<String> seven = new ArrayList<String>();
ArrayList<String> eight = new ArrayList<String>();
ArrayList<String> nine = new ArrayList<String>();
ArrayList<String> ten = new ArrayList<String>();
ArrayList<String> eleven = new ArrayList<String>();
ArrayList<String> twelve = new ArrayList<String>();
for (String str : lastfirst) {
int comma = str.indexOf(",");
if (str.substring(comma + 1) == "**Pre-K") {
prek.add(str);
}
if (str.substring(comma + 1) == "**Jr Pre-K") {
jrprek.add(str);
}
if (str.substring(comma + 1) == "*K") {
k.add(str);
}
if (str.substring(comma + 1) == "1") {
one.add(str);
}
if (str.substring(comma + 1) == "2") {
two.add(str);
}
if (str.substring(comma + 1) == "3") {
three.add(str);
}
if (str.substring(comma + 1) == "4") {
four.add(str);
}
if (str.substring(comma + 1) == "5") {
five.add(str);
}
if (str.substring(comma + 1) == "6") {
six.add(str);
}
if (str.substring(comma + 1) == "7") {
seven.add(str);
}
if (str.substring(comma + 1) == "8") {
eight.add(str);
}
if (str.substring(comma + 1) == "9") {
nine.add(str);
}
if (str.substring(comma + 1) == "10") {
ten.add(str);
}
if (str.substring(comma + 1) == "11") {
eleven.add(str);
}
if (str.substring(comma + 1) == "12") {
twelve.add(str);
return twelve;
}
}
for (String str : twelve) {
System.out.print(str + " ");
System.out.println();
}
return twelve;
}
public static void main(String[] args) {
method1(names);
if (!(method2(names).isEmpty())) {
JOptionPane.showMessageDialog(null, "List isn't empty");
} else {
JOptionPane.showMessageDialog(null, "List is empty!");
}
}
}