Results 1 to 11 of 11
Thread: ArrayList of ArrayLists
- 07-03-2008, 09:09 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 6
- Rep Power
- 0
ArrayList of ArrayLists
Hi all
I am new to Java. I am working with ArrayLists and have a couple of questions in that. So here goes..
#1. I have an ArrayList and I keep adding Strings to it. Upon a certain condition, I add this ArrayList to an ArrayList of ArrayLists like this
Java Code:class Test { ArrayList<String> arr = new ArrayList<String>(); ArrayList<ArrayList> arrOfArr = new ArrayList<ArrayList>(); public void startElement (String tagName) { arr.add(tagName); } public void endElement () { arrOfArr.add(arr); } public void removeLastElement () { arr.remove(arr.size() -1); } public static void main(String args[]) { Test test = new Test(); test.startElement("ABC"); test.startElement("DEF"); test.endElement(); test.removeLastElement(); test.removeLastElement(); } }
#2. In the above code, is there a way to convert arrOfArr to a String[][] without iterating through arrOfArr element by element?
- 07-03-2008, 09:32 AM #2
Member
- Join Date
- Jul 2008
- Posts
- 6
- Rep Power
- 0
Java Code:public static void main(String args[]) { Test test = new Test(); test.startElement("ABC"); test.startElement("DEF"); test.endElement(); test.removeLastElement(); test.removeLastElement(); }
Thanks in advance
~
- 07-03-2008, 09:39 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 07-03-2008, 09:40 AM #4
RE: ArrayList of ArrayLists
I am not sure what application you are trying to build. The code you have pasted here is not complete. Can you let us know in more detail what you require.
#2. In the above code, is there a way to convert arrOfArr to a String[][] without iterating through arrOfArr element by element?
From the second question it seems you are not a beginner at all.:)
- 07-03-2008, 09:44 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Srikanth, what you mean not complete. Any reason? I guess you are talking about packages. ;)
- 07-03-2008, 09:50 AM #6
Member
- Join Date
- Jul 2008
- Posts
- 6
- Rep Power
- 0
- 07-03-2008, 09:57 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Likewise you have refer the required element. Do you want to check that element is another array list or just an element before remove it?
- 07-03-2008, 10:01 AM #8
Not exactly. See the way the code has been implemented.
The elements have only been added and deleted in the first array. I was not sure what was the reason to use the second arraylist for the above implementation.
That is why I asked him to provide the exact testcase that he needs so that we can help him out
- 07-03-2008, 10:05 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Basically he try to add an array list within another. Second array list used to store the first.
Actually need a well explanation about the question too.
- 07-03-2008, 10:30 AM #10
Member
- Join Date
- Jul 2008
- Posts
- 6
- Rep Power
- 0
Ok. I am actually writing a plugin for a tool I am using at work. The plugin should parse an XML input file and return a 2D string array. The tool provides a class called Plugin, and I am extending this class to add my implementation.
I am using SAX Parser and the startElement() is triggered whenever a new tag is parsed in the XML File. I am adding the name of the tag to an ArrayList (arr). Whenever closing tag is encountered, endElement() is triggered by the parser. I am deleting the last added tag name in the ArrayList (arr). By doing this, I keep track of the path of the current tag from root tag in the input XML file. Also in the endElement() method, I am adding the current path stored in arr to arrOfArr. Hence, in the end, arrOfArr will have an array of the paths of each node from root node.
- 07-03-2008, 11:07 AM #11
Member
- Join Date
- Jul 2008
- Posts
- 6
- Rep Power
- 0
Ok. I have written a code and it works as I want it to. Can you please tell me if there are any better ways of doing it?
for #1, I did
Java Code:arrOfArr.add(arr); String tempStr[] = (String[])arr.toArray(new String[arr.size()]); arr = new ArrayList<String>( Arrays.asList(tempStr));
for #2, I did
Java Code:String[][] dataOutput = new String[row][col]; for(i=0; i<row; i++) { tempArr = arrOfArr.get(i); tSize = tempArr.size(); for(j=0; j<tSize; j++) { dataOutput[i][j]=tempArr.get(j).toString(); } }
Similar Threads
-
how to compare the elements of the two arraylists al1,al2
By raj reddy in forum Web FrameworksReplies: 33Last Post: 11-25-2009, 06:48 PM -
how to compare the elements of these two arraylists
By raj reddy in forum Web FrameworksReplies: 1Last Post: 03-25-2009, 11:55 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 12:43 PM -
A Map implemented with ArrayLists
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:29 PM -
arraylists problem
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-12-2008, 08:38 AM
Bookmarks