我想将一些数组对象放入集合应该怎么做
时间: 2023-12-20 10:09:53 浏览: 17
你可以使用Java中的ArrayList或者LinkedList集合来存储数组对象。下面是一个使用ArrayList的示例代码:
```
// 创建一个存储数组对象的ArrayList
ArrayList<int[]> arrayList = new ArrayList<>();
// 添加数组对象到ArrayList中
int[] array1 = {1, 2, 3};
arrayList.add(array1);
int[] array2 = {4, 5, 6};
arrayList.add(array2);
// 遍历ArrayList中的数组对象
for (int[] array : arrayList) {
for (int i : array) {
System.out.print(i + " ");
}
System.out.println();
}
```
在这个示例中,我们创建了一个ArrayList来存储int类型的数组对象。然后我们添加了两个数组对象到ArrayList中,并使用双重循环遍历ArrayList中的数组对象并打印每个元素。你可以根据需要修改示例代码来存储你的数组对象。
相关问题
数组放入list集合
可以使用Java中的ArrayList类来实现将数组放入list集合中。具体的代码如下所示:
```java
// 定义一个数组
String[] array = {"apple", "banana", "orange"};
// 将数组放入list集合中
List<String> list = new ArrayList<>(Arrays.asList(array));
```
这里使用了Arrays.asList()方法将数组转换为List集合,并将其作为ArrayList的构造函数参数创建一个新的ArrayList对象。这样就可以将数组中的元素放入list集合中了。
将数组里面的值全部放入set集合中
好的,您可以使用下面的代码将数组中的值全部放入set集合中:
```python
array = [1, 2, 3, 3, 4, 5, 5, 6]
set_array = set(array)
print(set_array)
```
输出结果为:
```
{1, 2, 3, 4, 5, 6}
```
这段代码将数组 `[1, 2, 3, 3, 4, 5, 5, 6]` 中的值放入了一个set集合中,并输出了set集合中的元素。由于set集合是无序且不允许重复元素的,所以输出结果中只有一个3和一个5。