problem with Vector.contains and arrays

Discussion in 'Java' started by Jan Biel, Jul 3, 2003.

  1. Jan Biel

    Jan Biel Guest

    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");
     
    Jan Biel, Jul 3, 2003
    #1
    1. Advertising

  2. Jan Biel wrote:
    > 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
     
    Grzegorz Stasica, Jul 3, 2003
    #2
    1. Advertising

  3. > 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

    --
    For a private reply to make sure to provide an
    interesting subject line.
     
    Wolf Martinus, Jul 3, 2003
    #3
  4. Jan Biel

    Ian McCall Guest

    "Jan Biel" <> wrote in message
    news:be16go$2mll$-clausthal.de...
    > Grzegorz Stasica wrote:
    > > My proposal is to resign from Vector and use Set instead. In set there
    > > are only unique objects

    >
    > 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
     
    Ian McCall, Jul 3, 2003
    #4
  5. Jan Biel wrote:
    > 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
     
    David Zimmerman, Jul 3, 2003
    #5
  6. Jan Biel

    Dale King Guest

    Jan Biel" <> wrote in message
    news:be15m8$2loo$-clausthal.de...
    > 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

    --
    Dale King
     
    Dale King, Jul 8, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Dyer
    Replies:
    5
    Views:
    687
  2. pmatos
    Replies:
    6
    Views:
    24,109
  3. Replies:
    8
    Views:
    1,996
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    618
    James Kanze
    Sep 4, 2007
  5. Philipp
    Replies:
    21
    Views:
    1,181
    Philipp
    Jan 20, 2009
Loading...

Share This Page