The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Storing Objects in a Vector

Posted by Christian on October 24, 2001 at 12:50 AM

Hi there,

I tried to use a Vector to store some simple Objects. The problem is, that I don't get the correct global variables of the Objects back. (see source code)
Instead of returning A and B, I just get B two times. I don't know the reason. I always get the name of the last added Object x the Vector size.
I can't figure out the problem - but maybe you can.

Thanks for all answers,

Chris.

public class VectorItem {

private static String Name;

public VectorItem(String ItemName) {

this.Name = ItemName;
}

public String getName() {

return this.Name;
}
}

---------------------------------------------------

import java.util.Vector;

public class Test {

private static Vector ObjectVector;

public static void show() {

for (int i = 0; i < ObjectVector.size(); i++) {

System.out.println( ((VectorItem) ObjectVector.elementAt(i)).getName());
}
}

public static void main(String [] argv) {

VectorItem v1 = new VectorItem("A");
VectorItem v2 = new VectorItem("B");

ObjectVector = new Vector();

ObjectVector.addElement(v1);
ObjectVector.addElement(v2);

show();
}
}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us