因此,对于我正在处理的程序,我试图创建一个将通过文本文件解析的方法。使用文本文件中的值,在ArrayList
内创建一个ArrayList
。这里是我的代码到目前为止:在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
,然后列出其相应的image
值在嵌套ArrayList
。但是,我有点卡住,不知道该怎么做。有人可以帮我吗?我感谢提供的任何帮助。
'Map>'可以更好地满足您的当前需求。按照这个答案:http://stackoverflow.com/questions/1010879/best-way-to-create-a-hashmap-of-arraylist/1011072#1011072 –
Marcelo
+1 @Marcelo - 甚至只是一个'地图<字符串,字符串>'如果rIdxx'键只有一个图像'值' – andyb
从示例数据中,一个简单的'Map'就足够了,因为在第一列中没有看到任何重复。 –