在我的Java应用程序中,我有一个类MyFileList
,它扩展了ArrayList并实现了TableModel。 MyFile
类
当我单击以对gui中的列进行排序时,我收到一个异常,并且列表被排除了一半。
Exception in thread "AWT-EventQueue-2" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.example.smdb.MyFileList.getValueAt(MyFileList.java:321)
at com.example.util.MyObjectListComparator.compare(MyObjectListComparator.java:97)
at java.util.TimSort.mergeHi(Unknown Source)
at java.util.TimSort.mergeAt(Unknown Source)
at java.util.TimSort.mergeForceCollapse(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.ArrayList.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at com.example.gui.MyTableSorter.sort(MyTableSorter.java:73)
at com.example.gui.MyTableSorter.sortByColumn(MyTableSorter.java:93)
at com.example.gui.PreservableColSizeJTable$PersistentJTableHeader.mouseClicked(PreservableColSizeJTable.java:935)
MyTableSorter
排序方法:
public void sort(int column) {
if(model instanceof List){
Collections.sort ((List)model, new MyObjectListComparator((List)model, column, ascending));
}
}
MyObjectListComparator
比较方法:
public int compare(Object obj, Object obj1) {
int result = 0;
int row = this.filelist.indexOf(obj);
int row1 = this.filelist.indexOf(obj1);
Object value = ((MyFileList) this.filelist).getValueAt(row, column);
value = ((MyFile.FileAttr) value).getAttrValue();
Object value1 = ((MyFileList) this.filelist).getValueAt(row1, column);
value1 = ((MyFile.FileAttr) value1).getAttrValue();
result = compareAttr(value, value1);
return result;
}
MyFile
等于方法:
public boolean equals(Object obj) {
if (obj instanceof MyFile) {
if (this.getAttribute(FILE_ID) != null) {
return this.getAttribute(FILE_ID).equals(((MyFile) obj).getAttribute(FILE_ID));
}
}
return false;
}
调试后,我看到this.filelist.indexOf(obj)
方法中的compare
正在返回-1
。如果我抓住这个异常并让它继续排序,我最终会在列表中复制一些项目并丢失一些项目。
我发现TimSort
中useLegacySortMerge
的一些references听起来非常相似,所以我尝试了Exception in thread "AWT-EventQueue-2" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.example.smdb.MyFileList.getValueAt(MyFileList.java:321)
at com.example.util.MyObjectListComparator.compare(MyObjectListComparator.java:97)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.legacyMergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.ArrayList.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at com.example.gui.MyTableSorter.sort(MyTableSorter.java:73)
at com.example.gui.MyTableSorter.sortByColumn(MyTableSorter.java:93)
at com.example.gui.PreservableColSizeJTable$PersistentJTableHeader.mouseClicked(PreservableColSizeJTable.java:935)
选项。这给了我相同的行为,略有不同的堆栈跟踪,所以不幸的是,这看起来不像问题。
onSaveInstanceState
这让我疯狂了好几天。任何见解?
答案 0 :(得分:0)
只是一个想法:你可以处理在文件列表中找不到行和行1的情况,假设两个缺席都是等效的,缺席比任何存在都“少”。如果不起作用,则反转1和-1。
public int compare(Object obj, Object obj1) {
int result = 0;
int row = this.filelist.indexOf(obj);
int row1 = this.filelist.indexOf(obj1);
if(row == -1){
if(row1 == -1){
return 0;
}
return -1;
}else if(row1 == -1){
return 1;
}
Object value = ((MyFileList) this.filelist).getValueAt(row, column);
value = ((MyFile.FileAttr) value).getAttrValue();
Object value1 = ((MyFileList) this.filelist).getValueAt(row1, column);
value1 = ((MyFile.FileAttr) value1).getAttrValue();
result = compareAttr(value, value1);
return result;
}