Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By eRaaaa
  • 1 Post By kaydell2

Thread: Adding an array to an arraylist

  1. #1
    drakeramoray is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Adding an array to an arraylist

    Here is my code:

    Java Code:
    public class Gameprj{
        public static void main(String args[])
        {
             ArrayList<int[]> al=new ArrayList<int[]>();
            int a[]={1,2,3,4};
            al.add(a);
            a[0]=5;
            a[1]=6;
            a[2]=7;
            a[3]=8;
            al.add(a);
            for(int[] ar: al)
            {
                for(int k=0;k<a.length;k++)
                {
                System.out.print(ar[k]+" ");
                }
               System.out.println(); 
             }
            
            
            
        }
    }
    I get the output as

    5 6 7 8
    5 6 7 8

    Shouldn't it be
    1 2 3 4
    5 6 7 8

    What am I doing wrong?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Adding an array to an arraylist

    Also at Java Programming Forums.

    @drakeramoray: If you are going to start a discussion at multiple places it would be good if you posted links at each of them to the others. That way everybody taking part in the discussion knows what else is being said. You should be aware that some people will not respond to cross posts for fear of wasting their time on something that has been satisfactorily addressed elsewhere, or where the discussion might have moved off in some other direction.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Adding an array to an arraylist

    Yes if you would create/add a new array after line 6 (a = new int[a.length];)
    Internal, the arraylist only references to the object/array a. With a[0] = .. you only change this array, but the reference is still the same (references to the same array/object).
    If you print the toString of the arraylist -> System.out.println(al); you will see, both objects which are added to the list, are the same
    drakeramoray likes this.

  4. #4
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Adding an array to an arraylist

    You're using the same array named "a" to put into the ArrayList twice. When you add values to the array the second time, you're replacing the values that you added the first time. Before you add elements to the array the second time, try the following line of code:

    Java Code:
    a = new int[4];
    This will create new array. This will work, but it would be easier to understand if you used a different variable name for the array than using "a" both times.
    drakeramoray likes this.

  5. #5
    drakeramoray is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Adding an array to an arraylist

    Quote Originally Posted by eRaaaa View Post
    Yes if you would create/add a new array after line 6 (a = new int[a.length];)
    Internal, the arraylist only references to the object/array a. With a[0] = .. you only change this array, but the reference is still the same (references to the same array/object).
    If you print the toString of the arraylist -> System.out.println(al); you will see, both objects which are added to the list, are the same
    Thanks a lot:)...that worked right:)

  6. #6
    drakeramoray is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Adding an array to an arraylist

    Quote Originally Posted by kaydell2 View Post
    You're using the same array named "a" to put into the ArrayList twice. When you add values to the array the second time, you're replacing the values that you added the first time. Before you add elements to the array the second time, try the following line of code:

    Java Code:
    a = new int[4];
    This will create new array. This will work, but it would be easier to understand if you used a different variable name for the array than using "a" both times.
    Sure:)...Thanks for the help:)..that worked right:)

Similar Threads

  1. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  2. Adding integer to arraylist
    By powerpravin in forum New To Java
    Replies: 2
    Last Post: 04-03-2011, 07:21 AM
  3. Adding elements to an ArrayList
    By ArcherSam in forum Advanced Java
    Replies: 7
    Last Post: 01-28-2011, 03:05 PM
  4. Adding books into arrayList
    By mwenchong in forum New To Java
    Replies: 9
    Last Post: 12-15-2010, 02:10 PM
  5. adding date in arraylist
    By katturv in forum New To Java
    Replies: 1
    Last Post: 10-23-2010, 06:23 PM

Posting Permissions

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