How to Search an Array in Java


In this article, we show how to search an array in Java.

Searching an array, no matter what language you are using, can be done with a for loop.

Searching arrays can always be done with a for loop.

An array is a list of items that starts at the index of 0 and increments by 1 with each item until the last item in the array.

If you create a for loop with the count starting at 0 and incrementing by 1, you match the array and, thus, can search the array.

Below is Java code in which we create an array of fruits and we search it to see if the word "banana" is in the array. If it is, we output that the word banana is in the array.

Java Output

The word banana is in the array

So we'll break down all the code shown above.

We create a class named Fruit.

We then create a public static variable of an array of Strings named fruits. We set it equal to a list of fruit items.

We then create a public static variable of type int named found and set it equal to 0. This is important because it will update in the for loop if a match is found. If a match isn't found, it stays at its value of 0.

We want both of these variables to be of static type, because it's linked to the class, not to an object. In this code, we don't even create an object, so only static variables will work.

We then create the main function, which is where the program will start executing. If you are running a single class, as we are doing in this case, it needs a main() function.

Now we get to the for loop. We create an int named i and set it equal to 0. This is because an array starts at an index of 0. Being that we want to match each element of an array, we start at the 0 element of the array. As long as the variable i is less than the length of the array, the for loop continues. Being that we want to go the length of the array minus 1, this is the statement we use. Since an array starts at the 0 element, the last element of an array is always the length of an array minus 1. We then increment i by 1 for each iteration of the for loop. This is because arrays increase by 1 at each index. It starts at 0, then the next element is 1, then the next element is 2, then the next element is 3... and so on.

Underneath this for loop, we create our if statement which searches for the particular word or string or character we are looking for.

In this case, I'm searching to see whether the word "banana" is found in the array. If it is, the public static int found that we created increments by 1. If it isn't found, the found variable doesn't increment and stays at its initialized value of 0.

We then run an if statement. In the if statment, we put the name of the array, which is fruits, followed by [i], so that it's fruits[i]. Because arrays are represented by the name of the array followed by the index of an array within brackets, we reference the array by using the term fruits[i]. When we first start the array at 0, this means that it's fruits[0]. Doing so, we get the value of each of the items in the fruits array. fruits[0] is equal to orange. fruits[1] is equal to apple. And so on. After this we use the equals() method to see whether the string in the array is equal to the string "banana". You always use the equals() method in Java to see whether 2 strings are equal in terms of the same content. In Java, you do not use "==" to see if 2 strings are the same. Rather, you use the equals() method to see if 2 strings are the same. If you are checking to see if 2 number values (such as integers, doubles, floats) are equal, then you would use the "==" operator. The "==" operator is used for number type comparision.

In the if statement, if the variable found is greater than 0, then the word "banana" is in the array. This, again, is because the variable found only increments if the word is in the array. If not, it remains 0. If found is not greater than 0, the string we are searching for, in this case "banana, is not in the array.

So this is all that is necessary to search an array in Java.

For loops always come in handy when you want to loop through any list of items, such as those found in arrays. With a for loop, you can loop through any array and then using an if statement, you can see whether the value in that array matches the value that you are searching for. Then you can do whatever you want to do.

So this is just a quick overview of searching arrays in Java.

HTML Comment Box is loading comments...