这是另一种解决方案。代码不完整。我们的想法是创建一个新数组,其中值是一个包含值及其原始位置的新类。然后你排序src并使用排序版本来查找重复项。当你找到一个,你就可以获得它原来的位置。
class EntryWithIndex {
private int index;
private Object value;
... constructor, getter, setters ....
}
List srcWithIndexSaved = new ArrayList();
int i=0;
for( Object o : src ){
srcWithIndexSaved.add(new EntryWithIndex(i++, o));
}
Collections.sort(srcWithIndexSaved, new Comparator() .... );
for( int i=0; i
if( i>0 ){
Object current = srcWithIndexSaved.get(i);
Object previous = srcWithIndexSaved.get(i-1);
if( current.getValue().equals(previous.getValue()) ){
... use current.getIndex() to lookup it up in the dest list.
}
}
}