Java 对象的二维数组列表排序
我有一个对象列表,我想在包含LocalDate的第一列对它进行排序 我用这个代码对它进行排序Java 对象的二维数组列表排序,java,sorting,object,arraylist,localdate,Java,Sorting,Object,Arraylist,Localdate,我有一个对象列表,我想在包含LocalDate的第一列对它进行排序 我用这个代码对它进行排序 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy"); formatter = formatter.withLocale( Locale.US ); ArrayList<ArrayList<Object>> cusKashf = new ArrayList<ArrayList&l
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy");
formatter = formatter.withLocale( Locale.US );
ArrayList<ArrayList<Object>> cusKashf = new ArrayList<ArrayList<Object>>();
cusKashf.addAll(new ReadBillDeateals().readSell(textField_4.getText()));
Collections.sort(cusKashf , new Comparator<ArrayList<Object>>() {
@Override
public int compare(ArrayList<Object> entry1, ArrayList<Object> entry2) {
return ((LocalDate.parse(entry1.get(0).toString(), formatter)).compareTo((LocalDate.parse(entry2.get(0).toString(), formatter))));
}
});
您试图比较的列表之一为空,请在获取项目0之前尝试使用try and catch或检查列表是否为空。在cusKashf列表中检查子列表是否为空
@Override
public int compare(ArrayList<Object> entry1, ArrayList<Object> entry2) {
if(Objects.isNull(entry1) || entry1.isEmpty()) return -1;
if(Objects.isNull(entry2) || entry2.isEmpty()) return -1;
return ((LocalDate.parse(entry1.get(0).toString(), formatter)).compareTo((LocalDate.parse(entry2.get(0).toString(), formatter))));
}
@覆盖
公共整数比较(ArrayList entry1,ArrayList entry2){
if(Objects.isNull(entry1)| | entry1.isEmpty())返回-1;
if(Objects.isNull(entry2)| | entry2.isEmpty())返回-1;
return((LocalDate.parse(entry1.get(0.toString(),格式化程序)).compareTo((LocalDate.parse(entry2.get(0.toString(),格式化程序)));
}
请提供问题的可编译示例。错误表明您正在请求空列表的第2项,但您的代码似乎没有这样做。您的代码似乎在请求列表的项目0(可能为空)。例外情况很明显:列表为空。确保相关列表不为空。没错,谢谢你的帮助
@Override
public int compare(ArrayList<Object> entry1, ArrayList<Object> entry2) {
if(Objects.isNull(entry1) || entry1.isEmpty()) return -1;
if(Objects.isNull(entry2) || entry2.isEmpty()) return -1;
return ((LocalDate.parse(entry1.get(0).toString(), formatter)).compareTo((LocalDate.parse(entry2.get(0).toString(), formatter))));
}