How to print an Array in Java without using Loop
Given an array arr in Java, the task is to print the contents of this array.
Loop method: The first thing that comes to mind is to write a for loop from i = 0 to n, and print each element by arr[i].
for(int i = 0; i < Array.length; i++)
System.out.println(Array[i]);
This article tells how to print this array in Java without the use of any loop.
For this, we will use toString() method of Arrays class in the util package of Java. This method helps us to get the String representation of the array. This string can be easily printed with the help of print() or println() method.
Example 1: This example demonstrate how to get the String representation of an array and print this String representation.
// Java program to get the String // representation of an array and print it import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Get the array int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Get the String representation of array String stringArr = Arrays.toString(arr); // print the String representation System.out.println("Array: " + stringArr); } } |
Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 2: This array can also be printed directly without creating a string.
// Java program to print an array // without creating its String representation import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Get the array int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Print the array System.out.println("Array: " + Arrays.toString(arr)); } } |
Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Recommended Posts:
- Java Program for Print Number series without using any loop
- Print a 2 D Array or Matrix in Java
- Check loop in array according to given constraints
- Flatten a Stream of Lists in Java using forEach loop
- Flatten a Stream of Arrays in Java using forEach loop
- Print modified array after multiple array range increment operations
- Print elements of an array according to the order defined by another array | set 2
- Print intermediate values in an array
- Program to print Sum Triangle for a given array
- Print array elements that are divisible by at-least one other
- C++ Program to print an Array using Recursion
- Program to print Sum of even and odd elements in an array
- Print all triplets in sorted array that form AP
- Iterative approach to print all permutations of an Array
- Print All Distinct Elements of a given integer array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



