list倒序删除
public static void main(String[] args) { List<Integer> nums = new ArrayList<Integer>(); nums.add(3); nums.add(5); nums.add(1); nums.add(0); System.out.println(nums); Collections.sort(nums); System.out.println(nums); for (int i = nums.size() -1; i >= 0; i--) { System.out.println(nums.get(i)); nums.remove(i); System.out.println(nums); } }
[3, 5, 1, 0]
[0, 1, 3, 5]
5
[0, 1, 3]
3
[0, 1]
1
[0]
0
[]
public static void main(String[] args) { List<Integer> successList=new ArrayList<>(); successList.add(5); successList.add(3); successList.add(6); System.out.println(successList); List<Integer> ascList1 = successList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()); System.out.println(ascList1); }
[5, 3, 6]
[6, 5, 3]
List<Integer> success=new ArrayList<>(); success.add(5); success.add(3); success.add(6); List<Integer> successList=new ArrayList<>(); successList.add(2); successList.add(0); successList.add(1); System.out.println(success); successList.stream().sorted(Comparator.reverseOrder()).forEach(successIndex ->success.remove(successIndex.intValue())); System.out.println(success);
[5, 3, 6]
[]