Results 1 to 3 of 3
- 11-28-2007, 12:14 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 4
- Rep Power
- 0
Can't access two vectors independantly?
Hi I haven't used Vectors before but I can't work out what I'm doing wrong here. I have a class called PointSequence which contains a Vector of Points (and some methods to work with them). If I then create two instances of this class (or an array of two for that matter), adding something to one of them seems to add it to both.
This produces the following output:Java Code:// A class for a sequence of Points import java.awt.*; import java.util.*; public class PointSequence { static Vector <Point> points=new Vector <Point> (); //the x,y locations are stored in a vector which will grow as you add items //i have some other methods here public static void main(String[] args) { //create two instances of the new class PointSequence testA=new PointSequence(); PointSequence testB=new PointSequence(); //add something to A testA.points.add(new Point(256,196)); //add something different to B testB.points.add(new Point(100,100)); //print out the contents of each instance System.out.println("A contains..."); for(int i=0;i<testA.points.size();i++) { System.out.println(testA.points.elementAt(i)); } System.out.println("B contains..."); for(int i=0;i<testB.points.size();i++) { System.out.println(testB.points.elementAt(i)); } } }
A contains...
java.awt.Point[x=256,y=196]
java.awt.Point[x=100,y=100]
B contains...
java.awt.Point[x=256,y=196]
java.awt.Point[x=100,y=100]
Why do both have the same contents even though I added one item to each?!! Is there something fundamental I am misunderstanding about Vectors or classes?
Cheers:confused:
- 11-28-2007, 06:08 PM #2
Marking the Vector with the static access modifier makes it a "class" object, ie, there is only one Vector object for each and every instance of PointSequence. Any instance can modify the vector and this modification will change the one "class" object for all instances. Removing the static access modifier from the declaration of the "points" Vector will allow each instance of PointSequence to have its own independent instance (object) of the vector.
For more discussion of this see Understanding Instance and Class Members.
Java Code:// Try running PointSequence with and again without // the [i]static[/i] access modifier for the Vector: /*static*/ Vector<Point> points = new Vector<Point>(); // Add this test at the end of your class and see what happens // when you make changes in the declaration of "points" above. // Are these two vectore the same or equal? System.out.printf("testA.points == testB.points = %b%n" + "testA.points.equals(testB.points) = %b%n", testA.points == testB.points, testA.points.equals(testB.points));
- 11-28-2007, 11:57 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Vectors of Vectors or hash-somethings?
By mindwarp in forum New To JavaReplies: 3Last Post: 03-10-2008, 02:57 PM -
Vectors vs ArrayList
By Java Tip in forum Java TipReplies: 0Last Post: 11-28-2007, 10:29 AM -
Understanding Vectors
By cbrown08 in forum New To JavaReplies: 7Last Post: 11-05-2007, 06:56 PM -
Help with access a database using Microsoft Access
By cachi in forum JDBCReplies: 1Last Post: 08-07-2007, 07:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks