Compare an object in an array with all objects in another array?

I have 2 arrays. The first one stores 100 JButtons and the second one stores 4 of those JButtons.
And I want an if-statement that compares one object in Array1 with the 4 objects in Array2.

This is along the lines of how I want it to look…


// r = random number.
if(array1[r] == array2[] )
{
//Do stuff
}

Any method or something I could use to look through every index of array2?

Maybe you want to compare objects using Object1.equals(Object2) for good results.

something like


// assuming:
// JButton buttonToSearchFor is the random button
// and the 4 Buttons are in: JButton[] theButtonArray
for(JButton testButton : theButtonArray) {
   if(buttonToSearchFor.equals(testButton)) {
      // Jump and dance
   }
}


Not sure exactly what your requirement is - are you saying you want to check that the 4 buttons in array2 are members of array1?

If so something along these lines:

final List<Jbutton> list = Arrays.asList( array1 );
for( JButton b : array2 ) {
    if( list.indexOf( b ) == -1 ) return false;
}
return true;

If your array1 is already sorted you could use:

Arrays.binarySearch( array1, b );

Wasn’t quite as simple as I thought.
But everything works “fine” now.

Thanks for the help!