Creating an ArrayList within an ArrayList
So for a program I am working on, I am trying to create a method which will parse through a text file. Using the values within the text file, create an ArrayList
within an ArrayL开发者_JS百科ist
. Here is my code so far:
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;
}
And here is an example of the text file I am looking at.
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
Essentially, I want to be able to split up the values, store all rId
values in the first ArrayList
, then list their corresponding image
values in the nested ArrayList
. However, I am kind of stuck and not sure what to do at this point. Could someone help me out? I appreciate any help that is offered.
I suggest you use a debugger to see exactly what is happening.
From reading the code I would guess it reads the first line okay but fails after that point because you only add one nested ArrayList but try to use many of them.
The simplest thing to do is to return Map<String, String> as I assume the first column is unique.
public static Map<String, String> getRels(String relsFile) {
Map<String, String> map = new LinkedHashMap<String, String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(relsFile));
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",", 2);
if (parts.length > 1)
map.put(parts[0], parts[1]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
} catch (IOException ignored) {
}
}
return map;
}
Create the two arraylists. Populate them .. then add them into your other arraylist.
That being said, does that have to be an arraylist? Can't you just have a regular object that contains two different arraylists ? Even better, maybe you want to consider a different data structure itself like a Map ?
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;
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
while ((strLine = br.readLine()) != null)
{
// Add values to array list
currentId = strLine.split(",");
list1.add(currentId[0]);
list2.add(currentId[1]);
}
//Close the input stream
in.close();
relsList.add(list1);
relsList.add(list2);
}
I think a better way of doing this task is to use any of the already existing libraries for reading from CSV files. This will make your code much simpler.
I agree with Peter L - definitely use a Map implementation. I used a TreeMap for testing purposes since it's a sorted Map, but you can use whichever Map you want. I also tested this to make sure it worked.
From your description, I believe this is what you want:
public TreeMap<String, ArrayList<String>> getRels(String relsFile)
{
String[] currentId;
int i = -1;
TreeMap<String, ArrayList<String>> relsList = new TreeMap<String, 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(",");
if(relsList.containsKey(currentId[0])) {
relsList.get(currentId[0]).add(currentId[1]);
} else {
relsList.put(currentId[0], new ArrayList<String>());
relsList.get(currentId[0]).add(currentId[1]);
}
}
//Close the input stream
in.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
return relsList;
}
public static void main(String[] args) {
ListReader lr = new ListReader();
TreeMap<String, ArrayList<String>> l = lr.getRels("test.txt");
for(String s : l.keySet()) {
for(String k : l.get(s)) {
System.out.println("Key: " + s + " Value: " + k);
}
}
}
What I've done in the parsing part is parse the file with a simple IF...ELSE statement. It checks to see if the key already exists (the rId part) and if it does, it simply adds the value on the right to the existing ArrayList. If not, it adds a new key and then creates an ArrayList, which it then adds the value to.
Hopefully this is what you want!
精彩评论