java用arrylist提取text文档中的每一行后用集合把array中的每一组数据中的字符串以逗号为界限存入
时间: 2024-05-27 16:10:42 浏览: 30
ArrayList<String> lines = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader("text.txt"));
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<ArrayList<String>> dataList = new ArrayList<>();
for (String str : lines) {
String[] arr = str.split(",");
ArrayList<String> data = new ArrayList<>(Arrays.asList(arr));
dataList.add(data);
}
// 打印 dataList 中的数据
for (ArrayList<String> data : dataList) {
for (String str : data) {
System.out.print(str + " ");
}
System.out.println();
}
相关问题
java 中arrylist中的字符串以逗号为界分开提取并存入集合
可以使用split函数将字符串以逗号为界分开,然后将分开的字符串存入集合中。
示例代码:
```java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
String str = "a,b,c,d,e,f"; // 定义字符串
String[] arr = str.split(","); // 将字符串以逗号为界分开,返回一个字符串数组
ArrayList<String> list = new ArrayList<>(); // 定义一个集合
for (String s : arr) { // 遍历字符串数组
list.add(s); // 将分开的字符串存入集合中
}
System.out.println(list); // 输出集合中的内容
}
}
```
输出结果:
```
[a, b, c, d, e, f]
```
使用Java代码实现一个ArryList集合中有一百多条数据每12条数据为一组最后剩下的为一组
可以使用以下代码实现:
```
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.add("data" + i);
}
int groupSize = 12;
int numOfGroups = list.size() / groupSize;
int remainder = list.size() % groupSize;
for (int i = 0; i < numOfGroups; i++) {
ArrayList<String> group = new ArrayList<>(list.subList(i * groupSize, (i + 1) * groupSize));
System.out.println("Group " + (i + 1) + ": " + group);
}
if (remainder > 0) {
ArrayList<String> lastGroup = new ArrayList<>(list.subList(numOfGroups * groupSize, list.size()));
System.out.println("Last group: " + lastGroup);
}
}
}
```
这段代码会将一百多条数据分成若干组,每组包含12条数据,最后剩下的数据为一组。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)