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 with 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.
To do this, we use the toString() method of the Array class in the Java utility package. This method helps us to get string representation of array. This string can be easily printed using the print() or println() methods.
Example 1: This example shows how to get a string representation for an array and print that 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); } }
Output:
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)); } }
Output:
Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.