我想知道如何将以下数据拆分为多个列表。这里是我的输入(来自文本文件,此处重新创建示例):将ArrayLists拆分为多个ArrayLists
aaaa bbbb cccc,ccc,cccc
aaaa-- bbbb
aaaa bbbb cccc-
aaaa bbbb cccc,ccc
aaaa-
aaaa bbbb ccc,cccc,cccc
分隔每段文本都是空白。我需要编写的代码应该创建三个列表,由每行文本文件中每个条目的a,b和c组组成,而忽略任何带有“ - ”的行。因此,如下我3列应填写:
Array1: aaaa, aaaa, aaaa
Array2: bbbb, bbbb, bbbb
Array3: (cccc,ccc,cccc),(cccc,ccc),(ccc,cccc,cccc)
括号里加入表明,第三阵应包括所有列出的C值 A,B和C都含有从文本文件导入的字符串。这是我的代码到目前为止:
import java.util.*;
import java.io.*;
public class SEED{
public static void main (String [] args){
try{
BufferedReader in = new BufferedReader(new FileReader("Curated.txt"));
String temp;
String dash = "-";
int x = 0;
List<String> list = new ArrayList<String>();
List<String> names = new ArrayList<String>();
List<String> syn = new ArrayList<String>();
List<String> id = new ArrayList<String>();
while((temp = in.readLine()) != null){
if(!(temp.contains(dash))){
list.add(temp);
if(temp.contains(" ")){
String [] temp2 = temp.split(" ");
names.add(temp2[0]);
syn.add(temp2[1]);
id.add(temp2[2]);
}else{
System.out.println(temp);
}//Close if
System.out.println(names.get(x));
System.out.println(syn.get(x));
System.out.println(id.get(x));
x++;
}//Close if
}//Close while
}catch (Exception e){
e.printStackTrace();
System.exit(99);
}//Close try
}//Close main
}//Close class
但我的输出总是:什么都没有。我怎样才能将这些值正确保存到3个独立的数组或列表中?
好,我会遍历所有元素我会用一个lo t的if和string的contains()来做到这一点。 – Leo
谢谢桑托斯先生,我能找到以下错误: java.lang.IndexOutOfBoundsException:指标:1,尺寸:1 \t在java.util.ArrayList.rangeCheck(ArrayList.java:604) \t在SEED.main(SEED.java:39) – user3781288
处,java.util.ArrayList.get(ArrayList.java:382) \t我不会在你的异常处理中使用System.exit(99) – Leo