String 强转为ArrayList<Emp>
时间: 2023-04-01 15:02:32 浏览: 102
可以使用以下代码将 String 强转为 ArrayList<Emp>:
String str = "emp1,emp2,emp3";
ArrayList<Emp> empList = new ArrayList<>();
String[] empArray = str.split(",");
for (String emp : empArray) {
empList.add(new Emp(emp));
}
其中,Emp 是一个自定义的类,用于表示员工信息。
相关问题
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 异常。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)