if(list.size() > 6) { Random randomId = new Random();
List<Integer> indexes = new ArrayList<Integer>();
while(indexes.size() < 6) {
int index = randomId.nextInt(list.size());
if(!indexes.contains(index)) {
indexes.add(index);
}
}
Collections.sort(indexes);
List<ListObject> newList = new ArrayList<ListObject>();
for(int index : indexes) {
newList.add(list.get(index));
}
list.clear();
list.addAll(newList);
}複製程式碼