|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-03-2008, 09:09 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 6
|
|
|
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
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();
}
}
In this code, when removeLastElement() is executed, the elements in arr as well as arrOfArr are removed. Is there a way to remove elements from arr but leave arrOfArr intact?
#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
|
|
Member
|
|
Join Date: Jul 2008
Posts: 6
|
|
public static void main(String args[])
{
Test test = new Test();
test.startElement("ABC");
test.startElement("DEF");
test.endElement();
test.removeLastElement();
test.removeLastElement();
}
I forgot to mention, I cant do arr = new ArrayList<String>(); after test.endElement(); as I want to further process this ArrayList and keep adding it to arrOfArr.
Thanks in advance
~
|
|

07-03-2008, 09:39 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
|
|
Originally Posted by coolnfunky_raj
In this code, when removeLastElement() is executed, the elements in arr as well as arrOfArr are removed. Is there a way to remove elements from arr but leave arrOfArr intact?
Refer the element with the index.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-03-2008, 09:40 AM
|
 |
Member
|
|
Join Date: Jul 2008
Location: Pune
Posts: 2
|
|
|
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
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
|
|
Srikanth, what you mean not complete. Any reason? I guess you are talking about packages. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-03-2008, 09:50 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 6
|
|
Originally Posted by Eranga
Refer the element with the index.
I am referring the element by index. I am finding the last element and removing it.
arr.remove(arr.size() -1);
|
|

07-03-2008, 09:57 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
|
|
|
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-03-2008, 10:01 AM
|
 |
Member
|
|
Join Date: Jul 2008
Location: Pune
Posts: 2
|
|
|
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
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-03-2008, 10:30 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 6
|
|
|
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
|
|
Member
|
|
Join Date: Jul 2008
Posts: 6
|
|
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
arrOfArr.add(arr);
String tempStr[] = (String[])arr.toArray(new String[arr.size()]);
arr = new ArrayList<String>( Arrays.asList(tempStr));
This solved the problem of arrOfArr getting deleted when arr.remove() was used to delete elements.
for #2, I did
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();
}
}
This helped me convert ArrayList of Arraylists to String[][]
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|