我在比较器上得到了问题 - 我需要对我的ArrayList<Integer> numbersToSort
进行排序,它保留数字 - 链接到competition.participant.get(numbers.get(index))
- 所以我有一个主对象 - 有一个参与者列表对象的竞赛。所以,我的ArrayList<Integer> numbersToSor
t应该保留我应该使用哪些数字competition.participants
。我想如何排序我的ArrayList<Integer> numbersToSort
(我稍后将提供给我的ListView适配器)的唯一方法是制作另一个“并行”List<Participants>
,其中我将从主对象,竞争对手和商店中复制它们的编号ArrayList<Integer> numbersToSort
,以便我可以稍后从排序的List<Participant> participants
组装新的ArrayList<Integer> sortedNumbers
。你还在那里:)Java - 比较器
public class ParticipantIndexComparator implements Comparator<Integer> {
final List<Participant> participants;
public ParticipantIndexComparator(ArrayList<Integer> numbersToSort) {
participants=new ArrayList<Participant>();
for (int i=0;i<numbersToSort.size();i++)
{ participants.add(i,competition.participant.get(numbersToSort.get(i))); participants.get(i).comparator=numbersToSort.get(i);}
}
@Override
public int compare(Integer i1, Integer i2) {
long l1 = participants.get(i1).kpTime.get(kpSelected);
//time from selected checkpoint
long l2 = participants.get(i2).kpTime.get(kpSelected);
return (long) compare(l1, l2);
}
}
很好,问题是最后return (long) compare(l1, l2);
- 是的,我知道这是不对的,但它是30日尝试:)请改正了什么问题
为什么要长时间?你有没有尝试删除演员?这看起来像递归调用。尝试返回(int)(l1-l2) – NormR
更正。需要比较传递给compare()方法的int值。返回i1 - i2。 – NormR
,因为我需要完全比较这两个长度 – user2976267