Linear Search

Searching from the front
A lost-and-found desk logs each item as it's turned in.
String[] found = {"umbrella", "phone", "scarf", "keys", "scarf"};
The method below reports where item first appears, as an index, or -1 when it doesn't appear.
public static int firstSeen(String[] found, String item)
{
for (int i = 0; i < found.length; i++)
{
if (found[i].equals(item))
{
return i;
}
}
return -1;
}
The call firstSeen(found, "scarf") checks "umbrella", "phone", and "scarf", and returns 2, with the early return ending the loop. A search for "wallet" checks all five and returns -1, a value no index can have.
The elements are String objects, so the test is found[i].equals(item), which compares the characters. The == operator would compare references, and it can be false for two different objects holding the same word. In an int array the elements are values, and == compares them correctly.
Searching from the back
The desk wants the most recent item that matches, so it searches from the last index down.
for (int i = found.length - 1; i >= 0; i--)
With that header and the same body, a search for "scarf" returns 4. A search from the front reports the first match, and one from the back reports the last.
An ArrayList<String> is searched the same way, with size() in place of length and get(i) in place of [i].
A 2D array
The desk stores items in a cabinet with drawers, one row per drawer.
String[][] cabinet = { {"hat", "glove"},
{"phone", "keys"},
{"book", "scarf"} };
A search visits each row and runs a linear search across it.
public static int drawerOf(String[][] cabinet, String item)
{
for (int row = 0; row < cabinet.length; row++)
{
for (int col = 0; col < cabinet[row].length; col++)
{
if (cabinet[row][col].equals(item))
{
return row;
}
}
}
return -1;
}
The call drawerOf(cabinet, "keys") checks "hat", "glove", and "phone" before it finds "keys", and it returns 1. This method reports only the row. A method that had to report the column too would need to return two numbers, for example in a two-element array.