1

I am trying to check if an array lies within a LinkedList or even Stack structure, as so:

LinkedList<int[]> list = new LinkedList();
int foo = new int[]{1,2,3};
int bar = new int[]{1,2,3};
list.addLast(bar);
if(list.contains(foo)) {
   System.out.print("I found foo!");
} else {
   System.out.print("I didn't find foo.");
}

this returns "I didn't find foo". Same result when I try if (Stack.search(foo) != -1) which would succeed if foo were found in the Stack.

How do I search a LinkedList or other Vector/List-type structure for a specific array?

gentlefish
  • 13
  • 3

4 Answers4

1

Currently as per the above code. You haven't added foo in the linked list. and you haven't initialized int array properly.

        LinkedList<int[]> list = new LinkedList();
        int[] foo = new int[]{1,2,3};
        int[] bar = new int[]{1,2,3};
        list.addLast(foo);
        list.addLast(bar);
        if(list.contains(foo)) {
           System.out.print("I found foo!");
        } else {
           System.out.print("I didn't find foo.");
        }
Zealous System
  • 2,080
  • 11
  • 22
1

Check out these questions: Comparing two integer arrays in java and equals vs Arrays.equals in Java

When dealing with arrays, == and equals() in Java both return whether the two arrays being compared are the same object, not whether their contents are the same. Arrays.equals() will compare the contents of the arrays.

The contains() method uses equals() to check if it has found the given object in the list, so that won't work here. You'll have to manually search the list like so:

boolean foundArray = false;
for (int[] array : list) {
    if (Arrays.equals(array, foo)) {
        foundArray = true;
        break;
    }
}
Community
  • 1
  • 1
SmashMaster
  • 111
  • 1
  • 9
0

You are searching if the array reference within your LinkedList is the same as the reference to foo. Instead, you need to compare the contents of the array using:

for (int[] array : list) {
    if (Arrays.equals(array, foo)) {
        System.out.print("I found foo!");
    }
}
camelsaucerer
  • 301
  • 3
  • 12
0

Since arrays inherit the default equals() implementation from Object, using contains() won't work as expected. You'll need to iterate over your collection and use the Arrays utility class to find a match like

LinkedList<int[]> list = new LinkedList();

int bar = new int[] {1,2,3};
list.addLast(bar);

boolean found = false;
int foo = new int[] {1,2,3};

for (int[] arr : list) {
   if (Arrays.equals(foo, arr)) {
     found = true;
     break; // search no more
   }
}

if (found) {
     System.out.print("I found foo!");
} else {
   System.out.print("I didn't find foo.");
}

Using arr1.equals(arr2) is similar to doing arr1 == arr2. To compare the actual array contents you should use Arrays.equals(arr1, arr2) instead.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • After seeing previous responses, this is exactly how I went through and implemented my code. – gentlefish Apr 14 '15 at 04:11
  • Note that someone had also posted about `Arrays.deepEquals()` (but removed the post later on). While it's not required here but if you ever start saving multi-dimensional arrays in your list or stack, use that to compare your nested arrays correctly as well. – Ravi K Thapliyal Apr 14 '15 at 04:18