All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Print Array In Java Using For Loop

Last Updated : Mar 11, 2024

How To Print Array In Java Using For Loop

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]);
        }
    }
}
  1. This java code demonstrates how to print an array in Java using a for loop, as you can see.
  2. The command import java.util.Arrays; is used to import the Arrays class from the Java.util package.
  3. Even though this import statement is there, we don't use it in the code that is provided.
  4. As the program's initial point, we declare the class as ArrayPrintingExample.
  5. We create the main method within this class, which serves as the program's first point of execution.
  6. We initialize an integer array called numbers in the main method with the numbers 101, 202, 303, 404, and 505.
  7. Using System.out.println(), we then print a message to the console.
  8. The message says that a for loop will be used to output the array.
  9. To traverse through each element of the numbers array, we use a for loop.
  10. The variable s, which has a starting value of 0, drives the loop until it reaches the length of the array (s numbers.length).
  11. We use numbers[s] inside the loop to get the element at the current index i.
  12. We then use System.out.println() to print this value to the console.
  13. 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.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪