problem with Vector.contains and arrays

J

Jan Biel

Hi there!

I've got a little problem:

I take an empty Vector
Vector test = new Vector();

and two Arrays with the same contents:
int a[] = {1,2};
int b[] = {1,2};

Now I add one Array to the Vector:
test.add(a);

and test whether the second Array is in the Vector:
if (test.contains(b))
System.out.println("yep!");

Of course the result is that Array b is not in the Vector, since although it
contains the same values as a, it bears a different signature.

My question: How can I implement a way of finding arrays with same values in
the Vector. My goal is to implement a Vector with many one dimensional
Arrays containing two values (like above). A new Array will only be added if
there's no Array with the same values in the array.

Is that possible?

Thanks a lot in advance,
Janbiel

PS: Source code in one piece:

Vector test = new Vector();
int a[] = {1,2};
int b[] = {1,2};
test.add(a);
if (test.contains(b))
System.out.println("ist drin");
 
G

Grzegorz Stasica

Jan said:
Hi there!

I've got a little problem:

I take an empty Vector
Vector test = new Vector();

and two Arrays with the same contents:
int a[] = {1,2};
int b[] = {1,2};

Now I add one Array to the Vector:
test.add(a);

and test whether the second Array is in the Vector:
if (test.contains(b))
System.out.println("yep!");

Of course the result is that Array b is not in the Vector, since although it
contains the same values as a, it bears a different signature.

My question: How can I implement a way of finding arrays with same values in
the Vector. My goal is to implement a Vector with many one dimensional
Arrays containing two values (like above). A new Array will only be added if
there's no Array with the same values in the array.

Is that possible?

Thanks a lot in advance,
Janbiel

PS: Source code in one piece:

Vector test = new Vector();
int a[] = {1,2};
int b[] = {1,2};
test.add(a);
if (test.contains(b))
System.out.println("ist drin");

Hi'

My proposal is to resign from Vector and use Set instead. In set there
are only unique objects
 
W

Wolf Martinus

My question: How can I implement a way of finding arrays with same values in
the Vector. My goal is to implement a Vector with many one dimensional
Arrays containing two values (like above). A new Array will only be added if
there's no Array with the same values in the array.

Instead of using Array you could write your own Class "Pair" and
override the equals method. Then the Collection#contains method will do
what you want.

BTW: If you want to make sure that each element is contained only once a
"Set" is what you want to use.

Wolf
 
I

Ian McCall

Jan Biel said:
This is not possible since I need to access the elements by index which is
not possible for Set, as far as I know.

Which JDK? If you're using 1.4, how about a LinkedHashSet? This would allow
access by index, as it provides predictable iteration order.


Cheers,
Ian
 
D

David Zimmerman

Jan said:
Hi there!

I've got a little problem:

I take an empty Vector
Vector test = new Vector();

and two Arrays with the same contents:
int a[] = {1,2};
int b[] = {1,2};

Now I add one Array to the Vector:
test.add(a);

and test whether the second Array is in the Vector:
if (test.contains(b))
System.out.println("yep!");

Of course the result is that Array b is not in the Vector, since although it
contains the same values as a, it bears a different signature.

My question: How can I implement a way of finding arrays with same values in
the Vector. My goal is to implement a Vector with many one dimensional
Arrays containing two values (like above). A new Array will only be added if
there's no Array with the same values in the array.


I believe that ArrayList.equals() will do what you need. Instead of
arrays, use ArrayLists
 
D

Dale King

Jan Biel said:
Hi there!

I've got a little problem:

I take an empty Vector
Vector test = new Vector();

and two Arrays with the same contents:
int a[] = {1,2};
int b[] = {1,2};

Now I add one Array to the Vector:
test.add(a);

and test whether the second Array is in the Vector:
if (test.contains(b))
System.out.println("yep!");

Of course the result is that Array b is not in the Vector, since although it
contains the same values as a, it bears a different signature.

My question: How can I implement a way of finding arrays with same values in
the Vector. My goal is to implement a Vector with many one dimensional
Arrays containing two values (like above). A new Array will only be added if
there's no Array with the same values in the array.

Is that possible?


There is a way to do it that is sort of unorthodox. You can create a
Predicate object like this:

import java.lang.util.Arrays;

class IntArrayEqualsPredicate
{
private final int[] target;
public IntArrayEqualsPredicate( int[] target )
{
this.target = target;
}
public bool equals( Object o )
{
return Arrays.equals( target, (int[])o );
}
}

This is a bit unorthodox since its equals method is not symmetric, but it
does work.

The Jakarata commons collections API actually provides its own abstraction
for doing this sort of thing using and actual Predicate interface instead of
requiring you to use equals in an unorthodox way. See:

http://jakarta.apache.org/commons/collections.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top