Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Adding an array to an arraylist

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding an array to an arraylist

    Here is my code:

    public class NewMain{
        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
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Adding an array to an arraylist

    a is a reference to an array. You added that reference to the list twice so, when you print the list's contents, you get the values of that array printed twice.

    If you want the list to contain references to two different arrays you will need to create two different arrays. At the moment you say "int a[e]={1,2,3,4};" which creates one of the arrays. Later you have to say "a=new int[]{5,6,7,8}" to create the other one - or "a=new int[4]" and fill in its elements one at a time.

    --- Update ---

    Also at Java Forums.

Similar Threads

  1. Adding key/value pairs from a HashMap in to an ArrayList
    By vb89 in forum Collections and Generics
    Replies: 0
    Last Post: February 26th, 2012, 10:36 PM
  2. Replies: 3
    Last Post: February 10th, 2012, 12:35 AM
  3. Somehow it's not adding to an ArrayList of ArrayLists.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2012, 05:09 PM
  4. adding an object to an arrayList
    By urpalshu in forum Object Oriented Programming
    Replies: 1
    Last Post: November 5th, 2011, 01:04 AM
  5. Help with ArrayList ( Adding certain elements together)
    By williamsant in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 09:32 AM