ToArray questions about the ArrayList

Category: Java SE
2009-08-13 08:56:59

Sponsored Links
UGStu [] a = new UGStu [stu.size ()];
stu.toArray (a); / / stu is an arraylist
a [14]. setGrade (10000);
/ / stu.set (10, new ; UGStu ("jianren", "366", "noman", 11111,3333));
for (int count = 0; count <a.length; count + +)
System.out.println (((UGStu) (stu . get (count))). toString ());
System.out.println (((UGStu) (a [count])). toString ());
ask why the above function toArray modified after passing a portion of the posterior circulation [14] the same output
through arraylist modify (comment section) is not the same as the contents of the output it? ?
trouble enthusiastic people to help answer thank you ~ ~ ~

Sponsored Links

2009-08-13 09:08:37

stu is a collection of type Map, then as key-value pairs form of preservation.
So you use stu.get (count) method to pass key value, "count" is the key.
keys and values ​​are not necessarily the same, you can not duplicate key, the value can be repeated.
correspondence between you might look here:

Map stu = new HashMap();
String key = "1";
String value = "3";
stu.put(key, value); // 
key = "2";
value = "1";
stu.put(key, value);
key = "3";
value = "2";
stu.put(key, value);


Map stu = new HashMap();
String key = "1";
String value = "1";
stu.put(key, value); // 
key = "2";
value = "2";
stu.put(key, value);
key = "3";
value = "3";
stu.put(key, value);

situation a result of this situation as you are now, the results are different.
case two fluke results.
2009-08-13 09:24:58

because you use stu.set (10, new UGStu ("jianren", "366", "noman", 11111,3333)); replaces the list of When an object reference,
you put the List is the equivalent index is the position of the object 10 are replaced by references to it.
That is, you put that original references deleted, and then add a new object reference, the object is through the new UGStu ("jianren", "366", "noman", 11111 , 3333) created.

while your array is a reference to all the objects inside has not changed,
so the output is still original.

That is, you pass executed stu.set (10, new UGStu ("jianren", "366", "noman", 11111,3333)); after , a, and stu in the same references have been incomplete.
stu one was replaced.
2009-08-13 09:58:38
implements Serializable try
2009-08-13 10:20:11
comments section meant to replace the original object with index 10 for the new UGStu object.
a [14]. setGrade (10000); modify subscript target grade of 14 is 10000
2009-08-13 10:40:57
I want to just put an ArrayList toArray method copy to copy the contents of the array, arguably do not modify the ArrayList or array can affect each other, but I found that after the experiment to modify the data through the array, print the contents of the ArrayList also modified accordingly , while the ArrayList object by modifying the data stored in the array data has not changed. More bizarre. . .
2009-08-13 11:15:47

UGStu[] a = new UGStu[stu.size()]; 
    stu.toArray(a); // stu is an arraylist 

After this code is executed, the array and List stu save a reference (you go into an object) is executed java heap the same object.

When you
actually removed this reference, and then modify its properties, then the list stu where objects that reference pointed to the object's properties have changed,
because they are the same object.

landlord may wish to change the way you look down through the list to modify the properties of the method:

    UGStu[] a = new UGStu[stu.size()]; 
    stu.toArray(a); // stu is an arraylist 
   <span style="color: #FF0000;">((UGStu)stu.get(10)).setGrade(10000);</span>
// stu.set(10,new UGStu("jianren","366","noman",11111,3333)); 
    for (int count = 0;count < a.length;count ++) 

Look at the output.
2009-08-13 11:55:34
Thank you for your help
proved both may be shared heap inside the object, but the strange thing is I used / / stu.set (10, new UGStu ("jianren" , "366", "noman", 11111,3333));
the object inside the ARRAYLIST after replacement by System.out.println (((UGStu) (a [count])). toString ()); output is actually replacing the previous contents of the object. Do not understand is why?
2009-08-14 12:30:53
not sure what you say is not tried to explain more clearly? Thank you
2009-08-14 12:47:14
you just re-set a property of that object, after the output is not the same. . .
2009-08-14 01:13:20
Thank you help ~ ~ ~ ~ seemingly understand
2009-08-14 01:37:38
toArray look realization will understand
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.