java 逗号字符串转list
时间: 2023-12-03 15:05:35 浏览: 111
Java8 将List<Integer> 转换成以逗号分割的String字符串
可以使用Java的split()方法将逗号分隔的字符串转换为字符串数组,然后再将数组转换为列表。以下是一个示例代码:
```java
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
String str = "apple,banana,orange";
List<String> list = Arrays.asList(str.split(","));
System.out.println(list);
}
}
```
在这个示例中,我们首先将逗号分隔的字符串赋值给变量str。然后,我们使用split(",")方法将字符串分割成字符串数组,并使用Arrays.asList()方法将数组转换为列表。最后,我们打印出列表。运行这段代码,你会得到一个包含"apple"、"banana"和"orange"的列表。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [[开发|java] 将一个Java字符串按逗号分割成一个列表(List)](https://blog.csdn.net/macaiyun0629/article/details/131363631)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [java中逗号分隔的字符串和List相互转换](https://blog.csdn.net/lengdaochuqiao/article/details/80194340)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [Java面试宝典-经典](https://download.csdn.net/download/aaronmorgan/8542797)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文