Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default 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();
           }
    }
    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?

  2. #2
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default

    Java 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


    ~

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  4. #4
    Join Date
    Jul 2008
    Location
    Pune
    Posts
    2
    Rep Power
    0

    Default 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.:)

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  6. #6
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default

    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.

    Java Code:
    arr.remove(arr.size() -1);

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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?

  8. #8
    Join Date
    Jul 2008
    Location
    Pune
    Posts
    2
    Rep Power
    0

    Default

    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

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  10. #10
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default

    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.

  11. #11
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default

    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));
    This solved the problem of arrOfArr getting deleted when arr.remove() was used to delete elements.



    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();
    	}				
    }
    This helped me convert ArrayList of Arraylists to String[][]

Similar Threads

  1. how to compare the elements of the two arraylists al1,al2
    By raj reddy in forum Web Frameworks
    Replies: 33
    Last Post: 11-25-2009, 05:48 PM
  2. how to compare the elements of these two arraylists
    By raj reddy in forum Web Frameworks
    Replies: 1
    Last Post: 03-25-2009, 10:55 PM
  3. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  4. A Map implemented with ArrayLists
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:29 PM
  5. arraylists problem
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 07:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •