gpt4 book ai didi

java使每一行成为数组或列表

转载 作者:行者123 更新时间:2023-12-01 10:45:45 26 4
gpt4 key购买 nike

我的文本文件中有以下文本:AAA:a,b,cbbb:d,f

如何制作一个数组列表,其中每个数组仅包含“:”后面的单词并且不包含逗号例如,array1 将是:[a b c]array2 将是 [d f]

到目前为止我有这段代码,但我得到的输出是:[aaa:a、b、c][bbb:d,f]

// this list will store all the created arrays
List<String[]> arrays = new ArrayList<String[]>();

// use a BufferedReader to get the handy readLine() function
BufferedReader reader = new BufferedReader(new FileReader("scor.txt"));


for (String line1 = reader.readLine(); line1 != null;
line1 = reader.readLine()) {
String[] floatStrings = line1.split(",");
String[] textline = new String[floatStrings.length];
for (int i = 0; i < textline.length; ++i) {
textline[i] = floatStrings[i];
}
arrays.add(textline);
}
for (String[] arr : arrays) {
System.out.println(Arrays.toString(arr));
}

}

我稍微改变了代码:

String[] textline = new String[floatStrings.length-1];
for (int i = 0; i < textline.length; ++i) {
String[] newTextlineWithoutFirst = Arrays.copyOfRange(floatStrings, 1, floatStrings.length);
textline[i] = newTextlineWithoutFirst[i];

}
arrays.add(textline);
}
for (String[] arr : arrays) {
System.out.println(Arrays.toString(arr));
}

}

最佳答案

首先尝试在 : 上拆分,然后在 上拆分 [1]...

 for (String line1 = reader.readLine(); line1 != null; line1 = reader.readLine()) {
String[] splitString = line1.split(":");
splitString = splitString[1].split(",");
String[] textline = new String[splitString.length];
for (int i = 0; i < textline.length; ++i) {
textline[i] = splitString[i];
}
arrays.add(textline);
}

关于java使每一行成为数组或列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34187892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com