Results 1 to 6 of 6
Thread: Adding an array to an arraylist
- 01-07-2013, 09:38 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 3
- Rep Power
- 0
Adding an array to an arraylist
Here is my code:
I get the output asJava 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(); } } }
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?
- 01-07-2013, 09:45 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 01-07-2013, 09:46 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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
- 01-07-2013, 09:51 PM #4
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
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:
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.Java Code:a = new int[4];
- 01-07-2013, 09:52 PM #5
Member
- Join Date
- Jan 2013
- Posts
- 3
- Rep Power
- 0
- 01-07-2013, 09:53 PM #6
Member
- Join Date
- Jan 2013
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Adding all elements of an Array together, not adding up correctly
By Teclis in forum New To JavaReplies: 1Last Post: 04-05-2011, 08:58 PM -
Adding integer to arraylist
By powerpravin in forum New To JavaReplies: 2Last Post: 04-03-2011, 07:21 AM -
Adding elements to an ArrayList
By ArcherSam in forum Advanced JavaReplies: 7Last Post: 01-28-2011, 03:05 PM -
Adding books into arrayList
By mwenchong in forum New To JavaReplies: 9Last Post: 12-15-2010, 02:10 PM -
adding date in arraylist
By katturv in forum New To JavaReplies: 1Last Post: 10-23-2010, 06:23 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks