Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-03-2008, 09:09 AM
Member
 
Join Date: Jul 2008
Posts: 6
coolnfunky_raj is on a distinguished road
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

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(); } }
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-03-2008, 09:32 AM
Member
 
Join Date: Jul 2008
Posts: 6
coolnfunky_raj is on a distinguished road
Code:
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


~
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-03-2008, 09:39 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by coolnfunky_raj View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-03-2008, 09:40 AM
srikanthkapila's Avatar
Member
 
Join Date: Jul 2008
Location: Pune
Posts: 2
srikanthkapila is on a distinguished road
Send a message via Yahoo to srikanthkapila
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-03-2008, 09:44 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-03-2008, 09:50 AM
Member
 
Join Date: Jul 2008
Posts: 6
coolnfunky_raj is on a distinguished road
Quote:
Originally Posted by Eranga View Post
Refer the element with the index.
I am referring the element by index. I am finding the last element and removing it.

Code:
arr.remove(arr.size() -1);
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-03-2008, 09:57 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-03-2008, 10:01 AM
srikanthkapila's Avatar
Member
 
Join Date: Jul 2008
Location: Pune
Posts: 2
srikanthkapila is on a distinguished road
Send a message via Yahoo to srikanthkapila
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-03-2008, 10:05 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-03-2008, 10:30 AM
Member
 
Join Date: Jul 2008
Posts: 6
coolnfunky_raj is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-03-2008, 11:07 AM
Member
 
Join Date: Jul 2008
Posts: 6
coolnfunky_raj is on a distinguished road
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
Code:
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
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(); } }
This helped me convert ArrayList of Arraylists to String[][]
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to compare the elements of the two arraylists al1,al2 raj reddy Web Frameworks 27 04-28-2008 05:13 AM
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 12:43 PM
how to compare the elements of these two arraylists raj reddy Web Frameworks 0 04-17-2008 04:12 PM
A Map implemented with ArrayLists Java Tip java.lang 0 04-16-2008 11:29 PM
arraylists problem newtojava7 New To Java 1 03-12-2008 08:38 AM


All times are GMT +3. The time now is 08:16 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org