Introduction: How to Use a While Loop to Iterate an Array in Java
Today I will be showing you how to use Java to create a While loop that can be used to iterate through a list of numbers or words. This concept is for entry-level programmers and anyone who wants to get a quick brush-up on Java Loops and arrays.
- An IDE (Some popular choices are “Eclipse” or “IntelliJ”)
- A new Java class to write the program
- Beginner level understanding of Java Syntax
Step 1: Create an Empty Java Class With a Main Method
The main method for a java class is what executes when you run the program through your IDE. Any functions within the brackets for the main method is executed automatically when the class is run. This is where you will want to start writing your beginner programs.
Step 2: Instantiate Your Array
We are going to start by creating Arrays in Java which are just like a list of a certain object type. They have a set length based on how you fill them when they are created. In the image above I have created an Array of type Int(non-decimals) filled with some numbers.
Step 3: Create a Variable to Store the Length of the Array
Create a variable to hold the length of each Array. We will need the length when we set up the loop because the loop needs to know where to stop. Otherwise, we will get an error for going over the length of the loop.
Step 4: Set Up While Loop
We will be using a While Loop for this example. The way the loop works is that as long as "x" is less than "i" the loop will continue to run. To trigger the condition to stop the loop "x" must become the same value or greater than "i". We can increment "x" so it will eventually hit the value of "i" and the loop will stop running, we will be using "i" that we created earlier which is the total length of the array.
Step 5: Completeing the While Loop
We need to set up a counter which in our case is “x” which we set to zero. We can then run the while loop if “x” is less than “i” (which is the length of the array). Then we print the item in the Array at position “x”, the value of "x" will continue to increase each time the loop runs because of "x = x + 1". As "x" increases each time the next item in the array corresponding to the "x" will print.
Step 6: Format the Array Output
I want to take a moment to talk about the formatting of the output from the previous step. When it comes to the output, “System.out.print()” prints the text to the screen when you run the program. “listNumbers[x]” gives the item in the array at position “x”, adding the + “ “ gives you a space in the output, to make sure the printed list is not all connected.
Step 7: Check the Completed Version
This image shows the completed version of the program for using a While Loop to iterate an Array. Doing "//" gives you the ability to write a comment, it is always good practice to label what each section of your code does.
Step 8: Compile and Run the Code
If everything worked with no issues and the same array was used, you should have ended up with the above output after you compile and run the code in your IDE.
Step 9: Congratulations
If all the steps were followed correctly you should have ended up with the output from the previous step. After this tutorial, you should have a basic understanding of iterating an array using a While Loop. This is just a simple guide on the while loop and arrays to help you get started into your journey of Java. An alternative exercise would be to create an array of the Java String object and iterate it, using the same style we used for the array of integers.
Common errors that may occur are:
- mixing up variables or forgetting brackets for classes or loops.
- You could go past the array length and get an out of bounds exception, depending on your counter.