Java中用list存储多个数组的示例
时间: 2023-11-17 17:06:45 浏览: 64
可以使用 `List<int[]>` 来存储多个数组,示例如下:
```java
List<int[]> arraysList = new ArrayList<>();
int[] array1 = {1, 2, 3};
int[] array2 = {4, 5, 6};
int[] array3 = {7, 8, 9};
arraysList.add(array1);
arraysList.add(array2);
arraysList.add(array3);
// 遍历输出
for (int[] array : arraysList) {
System.out.println(Arrays.toString(array));
}
```
输出结果为:
```
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
```
相关问题
Java中用list存储数组的示例
下面是一个Java中用List存储数组的示例:
```java
import java.util.ArrayList;
import java.util.List;
public class ListArrayExample {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
List<Integer> list = new ArrayList<>();
for (int i : array) {
list.add(i);
}
System.out.println("List containing array elements: " + list);
}
}
```
在这个示例中,我们首先定义了一个整数类型的数组 `array`,然后创建了一个空的 `ArrayList`,名为 `list`。接下来,我们使用 `for` 循环遍历数组中的每个元素,并将其添加到 `list` 中。最后,我们打印出存储在 `list` 中的元素。
输出结果应该为:
```
List containing array elements: [1, 2, 3, 4, 5]
```
java中用数组a表示的数组中第2个元素
在Java中,数组的下标从0开始,因此数组a中第2个元素的下标为1。可以通过以下方式访问第2个元素:
```
a[1]
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)