recently, I was trapped when I wrote a piece of code that didn't understand Java. Let's briefly describe the effect you want to achieve: I'm going to plug 28 bytevector into the secretvector, and the 28 bytevector have 10 values respectively, and the 28 bytevector are different from each other.
public static vector Reformchildsecret (vector filevector) {vector secretvector= new Vector (); Vector bytevector= new vector (); for (int i= 0; i<; i++) {bytevector.removeallelements ();//key see here for (int j= 0 ; j< 10; J + +) {bytevector.add (j, ((Vector) (filevector.get (j))). get (i)); Key look Here}secretvector.add (bytevector);//key see here}system.out.println (secretvector.size ()); return secretvector;}
So can this code actually be implemented? No!
The reason is Simple: Bytevector is an object reference that refers to an object that only points to the same vector class from beginning to End. One of the features of Java is that you cannot manipulate objects directly, but rather manipulate them through object references .
Use this reason to analyze the code:
1, each time the Bytevector refers to the content of the object is emptied;
2, and then into the bytevector reference point of the object into the 10 values;
3, after the plug, add the bytevector reference to the object that the secretvector reference points to;
The problem, however, is that the objectSecretvector refers to is loaded with only bytevector references (and the same bytevector reference ), not Bytevector The contents of the object to which the reference is directed . The content that the bytevector reference points to will change in each loop, so in the end, the contents of each element of the Secretvector are the same, because each element is the same bytevector reference , and the content referenced by Bytevector will eventually be the last value of add ().
In addition, there is a blog post to the Java object and reference is very clear, if the Java Foundation is weak can look at. Post Address:
Http://www.cnblogs.com/focusChen/articles/2497768.html
Java objects and references-a real problem