【发布时间】:2011-10-08 00:12:52
【问题描述】:
因此,对于我正在开发的程序,我正在尝试创建一个可以解析文本文件的方法。使用文本文件中的值,在ArrayList 中创建一个ArrayList。到目前为止,这是我的代码:
public ArrayList<ArrayList<String>> getRels(String relsFile)
{
String[] currentId;
int i = -1;
ArrayList<ArrayList<String>> relsList = new ArrayList<ArrayList<String>>();
relsList.add(new ArrayList<String>());
try
{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(relsFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null)
{
// Add values to array list
currentId = strLine.split(",");
relsList.get(++i).add(currentId[0]);
//???????????
}
//Close the input stream
in.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
return relsList;
}
这是我正在查看的文本文件的示例。
rId13,image3
rId18,image8
rId26,image16
rId39,image29
rId21,image11
rId34,image24
rId7,image1
rId12,image2
rId17,image7
rId25,image15
rId33,image23
rId38,image28
rId16,image6
rId20,image10
rId29,image19
rId24,image14
rId32,image22
rId37,image27
rId15,image5
rId23,image13
rId28,image18
rId36,image26
rId19,image9
rId31,image21
rId14,image4
rId22,image12
rId27,image17
rId30,image20
rId35,image25
本质上,我希望能够拆分值,将所有rId 值存储在第一个ArrayList 中,然后在嵌套的ArrayList 中列出它们对应的image 值。但是,我有点卡住了,不知道此时该怎么做。有人可以帮我吗?感谢您提供的任何帮助。
【问题讨论】:
-
Map<String, List<String>>将帮助您更多地满足您当前的需求。按照这个答案:stackoverflow.com/questions/1010879/… -
+1 @Marcelo - 如果 rIdxx
key只有一张图片value,甚至只是Map<String, String> -
从示例数据来看,一个简单的
Map<String, String>就足够了,因为我在第一列中看不到任何重复项。
标签: java arraylist nested text-files nested-class