这是迄今为止,虽然数组的大小需要改变的代码。我需要使用循环向数组添加值;要做到这一点,我想我需要使用嵌套的ArrayList。但是,我是否能够按照我已经完成的方式对ArrayList进行排序,或者有什么方法可以将其添加到此数组中?排序/创建嵌套ArrayList
public static void main(final String[] args) {
String[][] data = new String[][] {
new String[] { "Roy","3", "12" },
new String[] { "Daniel","5", "22" },
new String[] { "Joe","2", "3" },
new String[] { "Ryan","4", "4" },
new String[] { "Leroy","2", "5" },
new String[] { "Smith","1", "6" },};
Arrays.sort(data, new Comparator<String[]>() {
@Override
public int compare(String[] entry1, final String[] entry2) {
String st1 = entry1[2];
String st2 = entry2[2];
int n1 = Integer.parseInt(st1);
int n2 = Integer.parseInt(st2);
return Integer.compare(n2, n1);
}
});
for (String[] s : data) {
System.out.println(s[0] + " " + s[1]+ " " + s[2]);
}
}
申报'data'如'列表'(或'的ArrayList ')和使用'Collections.sort'排序。你甚至可以使用相同的比较器。 –
MPirious