new ArrayList<String>() 怎么直接设值
时间: 2024-04-12 15:26:59 浏览: 15
你可以使用 Arrays 类的 asList() 方法将元素直接传递给 ArrayList 构造函数进行初始化。以下是一个示例:
```java
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
System.out.println(list);
}
}
```
这将创建一个包含字符串 "A"、"B" 和 "C" 的 ArrayList,并将其打印出来。输出结果将是:[A, B, C]。请注意,asList() 方法返回的是一个固定大小的列表,不能进行添加或删除操作。如果你需要对列表进行修改,请使用其他方法,例如 add() 或 remove()。
相关问题
ArrayList<ArrayList<String>>
`ArrayList<ArrayList<String>>` 是一个嵌套的 ArrayList,表示一个二维字符串数组。
具体来说,`ArrayList<ArrayList<String>>` 是一个包含多个 `ArrayList<String>` 的集合。每个 `ArrayList<String>` 都可以看作是一个字符串数组,而整个 `ArrayList<ArrayList<String>>` 就是一个二维的字符串数组。
可以通过以下方式来使用 `ArrayList<ArrayList<String>>`:
```java
// 创建一个空的二维字符串数组
ArrayList<ArrayList<String>> nestedList = new ArrayList<>();
// 创建一个内部字符串数组
ArrayList<String> innerList1 = new ArrayList<>();
innerList1.add("element1");
innerList1.add("element2");
// 将内部字符串数组添加到二维数组中
nestedList.add(innerList1);
// 创建另一个内部字符串数组
ArrayList<String> innerList2 = new ArrayList<>();
innerList2.add("element3");
innerList2.add("element4");
// 将另一个内部字符串数组添加到二维数组中
nestedList.add(innerList2);
// 访问二维数组中的元素
String element = nestedList.get(0).get(1);
```
上述代码演示了如何创建和使用 `ArrayList<ArrayList<String>>`,通过使用 `get()` 方法可以访问内部的字符串数组,通过使用 `add()` 方法可以向二维数组中添加元素。
ArrayList<ArrayList<String>>用法
ArrayList<ArrayList<String>> 表示一个二维字符串数组,也就是一个字符串矩阵。它的用法如下:
1. 初始化一个二维字符串数组:
```
ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
```
2. 添加一个新行:
```
ArrayList<String> row = new ArrayList<String>();
matrix.add(row);
```
3. 向某行添加元素:
```
matrix.get(row_index).add(element);
```
4. 获取某个元素:
```
String element = matrix.get(row_index).get(column_index);
```
5. 遍历整个二维字符串数组:
```
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix.get(i).size(); j++) {
String element = matrix.get(i).get(j);
// 处理元素...
}
}
```
注意:在使用 ArrayList<ArrayList<String>> 时,需要先初始化外层的 ArrayList 之后,再初始化内层的 ArrayList。否则,在向内层 ArrayList 添加元素时,会抛出 NullPointerException 异常。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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)