How To Print Array In Java Using For Loop
Last Updated : Mar 11, 2024
In this article we will show you the solution of how to print array in java using for loop, a fundamental Java activity that enables you to show an array's contents on the console is printing an array with a for loop.
You can access and print an array element's values one at a time by iterating over each element in the array.
A for loop is frequently used to do this task since it offers an orderly and effective approach to traversing the array.
You must initialize a loop that iterates through the array's indexes one by one from the first index to the last index if you want to print an array in Java using a for a loop.
The System.out.println() method allows you to retrieve the element at the current index during each iteration and show its value.
Now we'll discuss the concept of utilising a for loop to print an array in Java.
Step By Step Guide On How To Print Array In Java Using For Loop :-
import java.util.Arrays; public class ArrayPrintingExample { public static void main(String[] args) { int[] numbers = {101, 202, 303, 404, 505}; // Print a message to indicate that we are printing the array using a for loop System.out.println("Printing the array while utilising the for loop:"); for (int s = 0; s < numbers.length; s++) { // Access the element at the current index using numbers[s] System.out.println(numbers[s]); } } }
- This java code demonstrates how to print an array in Java using a for loop, as you can see.
- The command import java.util.Arrays; is used to import the Arrays class from the Java.util package.
- Even though this import statement is there, we don't use it in the code that is provided.
- As the program's initial point, we declare the class as ArrayPrintingExample.
- We create the main method within this class, which serves as the program's first point of execution.
- We initialize an integer array called numbers in the main method with the numbers 101, 202, 303, 404, and 505.
- Using System.out.println(), we then print a message to the console.
- The message says that a for loop will be used to output the array.
- To traverse through each element of the numbers array, we use a for loop.
- The variable s, which has a starting value of 0, drives the loop until it reaches the length of the array (s numbers.length).
- We use numbers[s] inside the loop to get the element at the current index i.
- We then use System.out.println() to print this value to the console.
- The loop continues to repeat until all of the array's elements have been printed.
Conclusion :-
As a result, we have successfully learned the concept of utilizing a for loop to print an array in Java.
Additionally, we learned how to use a for loop in Java to print the elements of an integer array.
Every element of the array is printed to the console by using the loop's iteration and index-based referencing to access the elements.
The code illustrates a straightforward and efficient method for visualizing an array's contents, making it simple to verify and debug array data.
Programmers can efficiently visualize and manipulate array values by being aware of this method.
When working with array data structures, one of the core skills for Java developers is the ability to print arrays using a for loop.
I hope this article on how to print array in java using for loop helps you and the steps and method mentioned above are easy to follow and implement.