We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Java Vector (Read 765 times)
Java Vector
Jun 8th, 2007, 5:07pm
 
Hi,

I have a Java Vector class
Vector space = new Vector();

into which I store instances of a class.
space.addElement(newSpace(a,b,40,40,40));

The question is if there is a way to check if the space Vector contains a particular instance of the Space class.

I tried to use the .contains

if ( (space.contains(new Space(i,j,40,40,40)) ))

but it doesn't recognise it eventhough I know it's inside.
Any suggestions?

Re: Java Vector
Reply #1 - Jun 8th, 2007, 5:39pm
 
The answer to this has to do with how objects are stored and used in Java. For example after executing this line:
Code:
 Vector space = new Vector(); 



The "space" variable will not "store" the new vector, but is just a reference to this newly created object. Next, for example, consider doing something like this:
Code:
Vector space2 = space; 



You'll not have just created another Vector object, but have now 2 variables which refer to the same object. Makes sense?

Now the same happens in principle when you're adding a new element to this vector:
Code:
space.addElement(new Space(a,b,40,40,40)); 


The vector will now store a reference to the new Space object.

However, because of this if you want to check if the Vector already contains this object you'll really have to check for the reference. So rewrite it like that:
Code:
Space s=newSpace(a,b,40,40,40);
space.addElement(s);
...
if (space.contains(s)) ...


When you tried checking the vector in your version, you were creating another separate object (with the same values, but nevertheless with a different reference) and hence the check is always negative!

hth!
Re: Java Vector
Reply #2 - Jun 8th, 2007, 5:48pm
 
Thanks a lot! it's all clear now!
Re: Java Vector
Reply #3 - Jun 12th, 2007, 9:29am
 
The important thing to recognize here is the difference between Java's "==" operator and the .equals() function.

In particular, if you look in the Java documentation, you'll see that the .contains() method of the Vector class actually compares objects by using the .equals() method of the Object class. By default (as defined in the Object class, which is the ancestor of every Java object), a.equals(b) if and only if a == b, i.e. they are the same object reference as Toxi mentioned.

However, you can cause your Vector to use a less restrictive test for equality by overriding the default .equals() method in your Space object. For example, you might want to do something like the following:

Code:

//... within the definition of your Space object...
public boolean equals(object o) {
if (o.getClass() != Space.class) return false;
Space s = (Space)o;
if (o.size == this.size &&
o.somethingElse == this.somethingElse) {
return true;
} else {
return false;
}
}


Once you have implemented a correct .equals() function, the code you originally posted should work.
Re: Java Vector
Reply #4 - Jun 14th, 2007, 3:53pm
 
this is what I have done to address my  issue, only it was not within my Space class but as a public boolean function. Your's is way nicer though.

Thanks guys Smiley
Page Index Toggle Pages: 1