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?

1
  • That way you can compare primitive values not an array. Commented Apr 14, 2015 at 4:01

4 Answers 4

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.");
        }
Sign up to request clarification or add additional context in comments.

Comments

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;
    }
}

Comments

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.

2 Comments

After seeing previous responses, this is exactly how I went through and implemented my code.
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.
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!");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.