Loop Through Array for Element
In this step, you will learn how to check if a specific element exists within a Java array by iterating through it using a traditional for
loop. This is a fundamental technique for searching within collections of data.
First, let's create a new Java file named ArraySearch.java
in your ~/project
directory. You can do this using the WebIDE file explorer on the left or by running the following command in the terminal:
touch ~/project/ArraySearch.java
Now, open the ArraySearch.java
file in the WebIDE editor and add the following Java code:
public class ArraySearch {
public static void main(String[] args) {
// Define an array of strings
String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
// The element we want to search for
String searchElement = "Cherry";
// Variable to keep track if the element is found
boolean found = false;
// Loop through the array
for (int i = 0; i < fruits.length; i++) {
// Check if the current element is equal to the search element
if (fruits[i].equals(searchElement)) {
found = true; // Element found
break; // Exit the loop since we found the element
}
}
// Print the result
if (found) {
System.out.println(searchElement + " was found in the array.");
} else {
System.out.println(searchElement + " was not found in the array.");
}
}
}
Let's break down this code:
String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
: This line declares and initializes a string array named fruits
with several fruit names.
String searchElement = "Cherry";
: This line declares a string variable searchElement
and assigns the value "Cherry" to it. This is the element we are looking for in the array.
boolean found = false;
: This line declares a boolean variable found
and initializes it to false
. We will set this to true
if we find the searchElement
in the array.
for (int i = 0; i < fruits.length; i++)
: This is a standard for
loop that iterates through the array. i
starts at 0 and goes up to (but not including) the length of the fruits
array.
if (fruits[i].equals(searchElement))
: Inside the loop, this if
statement checks if the current element of the array (fruits[i]
) is equal to the searchElement
. We use the .equals()
method to compare strings in Java, not the ==
operator.
found = true;
: If the elements are equal, we set the found
variable to true
.
break;
: Once the element is found, we use the break
statement to exit the loop early, as there's no need to continue searching.
- The final
if/else
block prints a message indicating whether the searchElement
was found based on the value of the found
variable.
Save the ArraySearch.java
file (Ctrl+S or Cmd+S).
Now, let's compile and run the program in the terminal. Make sure you are in the ~/project
directory.
Compile the code:
javac ArraySearch.java
If there are no errors, a ArraySearch.class
file will be created.
Run the compiled code:
java ArraySearch
You should see the following output:
Cherry was found in the array.
Now, try changing the searchElement
to something that is not in the array, like "Grape"
, save the file, recompile, and run it again to see the different output.