Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

list of arrays

Thread view: 
Jeremy Watts - 02 Jan 2007 18:24 GMT
I have created a List with the intention of putting elements of
one-dimensional arrays within it.  The arrays contain integers.  The lines
I've used are:-

(for the purposes of this, I have set variableNumber equal to 5)

List<int[]>combinationList = new ArrayList<int[]>();
int[] degrees = new int[variableNumber + 1];

{ some routine which fills each element of 'degrees' with a random integer}

then, I wish to add the newly filled 'degrees' to the end of the list, using
:-
combinationList.add(degrees);

theres no problem just doing this the once, but if I attempt to add another
instance of 'degrees' to the list using the same above statement, then it
seems to overwrite the initial instance of degrees with the new one.

Why is it doing this?  I cant seem to find a way around it.  If I run
through say 10 iterations of the above then I am simply left with a list of
10 arrays all of which are identical to the last generated instance of
'degrees'.

Thanks
Daniel Pitts - 02 Jan 2007 18:51 GMT
> I have created a List with the intention of putting elements of
> one-dimensional arrays within it.  The arrays contain integers.  The lines
[quoted text clipped - 21 lines]
>
> Thanks

If you gave us an sscce <http://www.physci.org/codes/sscce/> , we'd be
better able to help you.  However, I can guess at what you are doing
wrong...

public class MyArrays {
   public static void main(String..args) {
       /* create the list object */
       List<int[]> list = new ArrayList<int[]>();

       /* create the array object */
       int[] degrees = new int[5];

       /* add the array object to the list */
       list.add(degrees);

       /* adds the SAME array object to the list */
       list.add(degrees);

       /* create a different array object */
       int[] moreDegrees = new int[5];

       /* add the new array object to the list */
       list.add(moreDegrees);
   }
}

I'm guessing that your problem is that you are only ever creating a
single int[] array object, and you are adding that same object to the
list multiple times.  list.add(foo) in reality only adds a reference to
foo -- not a copy of foo -- to the list. So, if you call list.get(0),
and list.get(1), you will get the SAME object.

Hope this helps.
Daniel.
Eric Sosman - 02 Jan 2007 19:35 GMT
Jeremy Watts wrote On 01/02/07 13:24,:
> I have created a List with the intention of putting elements of
> one-dimensional arrays within it.  The arrays contain integers.  The lines
[quoted text clipped - 19 lines]
> 10 arrays all of which are identical to the last generated instance of
> 'degrees'.

   You are probably doing this:

    int[] degrees = new int[variableNumber + 1];
    for (int i = 0;  i < 10;  ++i) {
       // fill degrees[] with data
       combinationList.add(degrees);
    }

... when you ought to be doing this:

    for (int i = 0;  i < 10;  ++i) {
       int[] degrees = new int[variableNumber + 1];
       // fill degrees[] with data
       combinationList.add(degrees);
    }

   The first version creates one array, fills it with numbers
adds it to the List, re-fills the exact same array with another
batch of numbers, adds it to the List a second time, ...  At
the end, there's still only one array and it contains only the
last set of values you placed in it.  Oh, and that same array
appears ten times in the List.

   The second version creates an array, fills it with numbers
and adds it to the List, then creates a second array, fills that
array with numbers and adds it to the List, ...  At the end,
there are ten independent arrays, each with its own set of
values, and each appearing once in the List.

   If that's not the problem, please post a short, complete
example that demonstrates your difficulty.

Signature

Eric.Sosman@sun.com

Jeremy Watts - 03 Jan 2007 07:27 GMT
> Jeremy Watts wrote On 01/02/07 13:24,:
> > I have created a List with the intention of putting elements of
[quoted text clipped - 35 lines]
>     // fill degrees[] with data
>     combinationList.add(degrees);

indeed i was.... shortly discovered after posting this :)   thanks for the
reply anyway

> }
>
[quoted text clipped - 16 lines]
> --
> Eric.Sosman@sun.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.