This program reads a lot of integers, in which may be duplicated numbers. The program picks out all the duplicated ones and sorts the remainders in a descendent order. 函数接口定义: public static ArrayList<Integer> pick(ArrayList<Integer> a); a is the ArrayLis
时间: 2023-07-02 12:09:30 浏览: 258
t containing the integers to be processed. The function should return an ArrayList<Integer> with all the duplicated numbers removed and the remaining numbers sorted in a descending order.
以下是可能的实现方式:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
public class Duplicates {
public static ArrayList<Integer> pick(ArrayList<Integer> a) {
// Use HashSet to remove duplicates
HashSet<Integer> set = new HashSet<>(a);
// Convert back to ArrayList
ArrayList<Integer> result = new ArrayList<>(set);
// Sort in descending order
Collections.sort(result, Collections.reverseOrder());
return result;
}
}
```
这个函数使用了 HashSet 来去除重复的数字,然后将结果转换回 ArrayList,最后使用 `Collections.sort()` 方法对数组进行排序。因为要按照降序排序,所以传递了 `Collections.reverseOrder()` 参数。
阅读全文