ArrayList<Integer> ints = new ArrayList<Integer>(); 这是什么数组
时间: 2024-06-02 21:11:24 浏览: 7
这不是数组,而是 Java 中的 ArrayList。它是一个动态数组,可以根据需要动态增加或删除元素。在这个例子中,ints 是一个 ArrayList 对象,它可以包含任意数量的 Integer 类型的元素。通过调用 add 方法,可以将元素添加到列表的末尾,而通过调用 remove 方法,可以从列表中删除元素。与普通数组相比,ArrayList 具有更高的灵活性和可扩展性。
相关问题
ArrayList<Integer>
An ArrayList is a class in Java that provides a dynamic array implementation. It is part of the Java Collections Framework and is used to store a collection of objects. The ArrayList class is a generic class that can be used to store objects of any type.
The ArrayList<Integer> denotes an ArrayList that can only store objects of the Integer type. This is achieved through the use of Java generics, which allows the type of objects stored in a collection to be specified at compile time.
Here's an example of how to create an ArrayList<Integer>:
```
ArrayList<Integer> myList = new ArrayList<Integer>();
```
This creates a new ArrayList that can store objects of the Integer type. The ArrayList is initially empty, and elements can be added using the `add()` method:
```
myList.add(1);
myList.add(2);
myList.add(3);
```
This adds the integers 1, 2, and 3 to the ArrayList. The `size()` method can be used to get the number of elements in the ArrayList:
```
int size = myList.size(); // size is now 3
```
Elements can be retrieved from the ArrayList using the `get()` method:
```
int element = myList.get(0); // element is now 1
```
This retrieves the first element in the ArrayList (at index 0) and stores it in the `element` variable.
Duplicated Numbers 分数 10 作者 翁恺 单位 浙江大学 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 ArrayList to be parsed and returns the result as an ArrayList. Hint: These classes and functions may be used.
To solve this problem, you can use a HashMap to store the frequency of each number in the ArrayList. Then, iterate through the HashMap to find the numbers that occur more than once and add them to a new ArrayList. Finally, sort the new ArrayList in descending order using Collections.sort() with a custom comparator.
Here's an implementation:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class DuplicatedNumbers {
public static ArrayList<Integer> pick(ArrayList<Integer> a) {
HashMap<Integer, Integer> frequencyMap = new HashMap<>();
for (int num : a) {
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
}
ArrayList<Integer> duplicatedNumbers = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
if (entry.getValue() > 1) {
duplicatedNumbers.add(entry.getKey());
}
}
Collections.sort(duplicatedNumbers, (a1, a2) -> a2 - a1);
return duplicatedNumbers;
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)