Results 1 to 10 of 10
  1. #1
    logicraju is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default 2Dimensional Array List : Getting Mad !!

    Hi,

    First of all, I'm an ametuer programmer. Recently, a situation arised for which I had to use a 2dimensional arraylist in my program. But, I had a silly problem which kept me stuck (and is still keeping me stuck) for a while.

    The scenario: I want to insert two 1Dimensional arraylists into a 2Dimensional arraylist.

    The code:
    import java.io.*;
    import java.lang.*;
    import java.util.*;

    public class Array_Test
    {
    public static void main(String[] args)
    {
    ArrayList<Integer> data_list_1D = new ArrayList<Integer>();
    ArrayList<ArrayList<Integer>> data_list_2D = new ArrayList<ArrayList<Integer>>();

    data_list_1D.add(1);
    data_list_1D.add(2);
    data_list_2D.add(data_list_1D);

    data_list_1D.clear();

    data_list_1D.add(3);
    data_list_1D.add(4);
    data_list_1D.add(5);
    data_list_2D.add(data_list_1D);




    //////////////////////////////////////////////////////////
    // Displaying Array List //
    //////////////////////////////////////////////////////////
    System.out.println("1D List: "+data_list_1D);
    System.out.println("2D List: "+data_list_2D);

    }
    }

    Expected Output:
    [[1,2],[3,4,5]]

    Actual Output:
    [[3,4,5],[3,4,5]]

    ------------------------------------------------------------------------

    I'm getting mad, really mad !!!! I can't figure out what's going on. I know it's silly. Can somebody please help me solve this problem ??

    Thanks in advance !

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: 2Dimensional Array List : Getting Mad !!

    The error because both element you are adding to the data_list_2D variable are in fact referring to the same object of data_list_1D. If you change the value of data_list_1D it will reflect in both element of the data_list_2D. To avoid this you should create a new ArrayList before adding it to the data_list_2D.

  3. #3
    logicraju is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: 2Dimensional Array List : Getting Mad !!

    @wsaryada: Thank you very much ! So, do I have to create a new 1D arraylist each time I want to insert data ?

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: 2Dimensional Array List : Getting Mad !!

    Yes, you have to. Otherwise you'll face the same problem again.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: 2Dimensional Array List : Getting Mad !!

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    logicraju is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: 2Dimensional Array List : Getting Mad !!

    @ wsaryada: Thanks again for the reply !

    Maybe i'm stupid, I could'nt implement the solution. Could you please give me a sample code of what you mentioned, so that I can understand it more clearly.

    Thanks in advance !

  7. #7
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: 2Dimensional Array List : Getting Mad !!

    You can replace this line:

    Java Code:
    data_list_1D.clear();
    with

    Java Code:
    data_list_1D = new ArrayList<Integer>();

  8. #8
    logicraju is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: 2Dimensional Array List : Getting Mad !!

    I modified the code as follows:

    import java.io.*;
    import java.lang.*;
    import java.util.*;

    public class Graph_Test
    {
    public static void main(String[] args)
    {
    ArrayList<ArrayList<Integer>> data_list_2D = new ArrayList<ArrayList<Integer>>();

    ArrayList<Integer> data_list_Dummy1 = new ArrayList<Integer>();
    data_list_Dummy1.add(1);
    data_list_Dummy1.add(2);
    data_list_2D.add(data_list_Dummy1);

    ArrayList<Integer> data_list_Dummy2 = new ArrayList<Integer>();
    data_list_Dummy2.add(3);
    data_list_Dummy2.add(4);
    data_list_Dummy2.add(5);
    data_list_2D.add(data_list_Dummy2);


    //////////////////////////////////////////////////////////
    // Displaying Array List //
    //////////////////////////////////////////////////////////
    System.out.println("2D List: "+data_list_2D);

    }
    }

    Now, the output is:

    [[1,2],[3,4,5]]

    as expected. But, it is very inconvenient to create a 1D arraylist each time to insert data. Is there any way to use the same 1D arraylist for inserting data ?

    Thanks in advance !

  9. #9
    logicraju is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: 2Dimensional Array List : Getting Mad !!

    Alright, thanks !

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: 2Dimensional Array List : Getting Mad !!

    Quote Originally Posted by logicraju View Post
    I modified the code as follows:

    import java.io.*;
    import java.lang.*;
    import java.util.*;

    public class Graph_Test
    {
    public static void main(String[] args)
    {
    ArrayList<ArrayList<Integer>> data_list_2D = new ArrayList<ArrayList<Integer>>();

    ArrayList<Integer> data_list_Dummy1 = new ArrayList<Integer>();
    data_list_Dummy1.add(1);
    data_list_Dummy1.add(2);
    data_list_2D.add(data_list_Dummy1);

    ArrayList<Integer> data_list_Dummy2 = new ArrayList<Integer>();
    data_list_Dummy2.add(3);
    data_list_Dummy2.add(4);
    data_list_Dummy2.add(5);
    data_list_2D.add(data_list_Dummy2);


    //////////////////////////////////////////////////////////
    // Displaying Array List //
    //////////////////////////////////////////////////////////
    System.out.println("2D List: "+data_list_2D);

    }
    }

    Now, the output is:

    [[1,2],[3,4,5]]

    as expected. But, it is very inconvenient to create a 1D arraylist each time to insert data. Is there any way to use the same 1D arraylist for inserting data ?

    Thanks in advance !
    More than an hour ago, I gave you a couple of links to go through. Evidently, you didn't bother.

    Any more unformatted code and you are liable to be banned for a period.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Array List
    By turtlish in forum New To Java
    Replies: 10
    Last Post: 12-20-2011, 08:48 PM
  2. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  3. Array List help please
    By keo in forum New To Java
    Replies: 5
    Last Post: 10-17-2010, 08:00 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Array List help
    By Weejee37 in forum New To Java
    Replies: 4
    Last Post: 10-27-2009, 12:32 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
  •