Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you're asking.
Examples:
Simple Array:
String[] array = new String[] {"John", "Mary", "Bob"}; System.out.println(Arrays.toString(array));Output:
[John, Mary, Bob]Nested Array:
String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}}; // Gives undesired output: System.out.println(Arrays.toString(deepArray)); // Gives the desired output: System.out.println(Arrays.deepToString(deepArray));Output:
[[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922] [[John, Mary], [Alice, Bob]]doubleArray:double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 }; System.out.println(Arrays.toString(doubleArray));Output:
[7.0, 9.0, 5.0, 1.0, 3.0 ]intArray:int[] intArray = { 7, 9, 5, 1, 3 }; System.out.println(Arrays.toString(intArray));Output:
[7, 9, 5, 1, 3 ]
W3Schools
w3schools.com โบ java โบ java_arrays.asp
Java Arrays
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
Top answer 1 of 16
3267
Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you're asking.
Examples:
Simple Array:
String[] array = new String[] {"John", "Mary", "Bob"}; System.out.println(Arrays.toString(array));Output:
[John, Mary, Bob]Nested Array:
String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}}; // Gives undesired output: System.out.println(Arrays.toString(deepArray)); // Gives the desired output: System.out.println(Arrays.deepToString(deepArray));Output:
[[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922] [[John, Mary], [Alice, Bob]]doubleArray:double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 }; System.out.println(Arrays.toString(doubleArray));Output:
[7.0, 9.0, 5.0, 1.0, 3.0 ]intArray:int[] intArray = { 7, 9, 5, 1, 3 }; System.out.println(Arrays.toString(intArray));Output:
[7, 9, 5, 1, 3 ]
2 of 16
442
Always check the standard libraries first.
import java.util.Arrays;
Then try:
System.out.println(Arrays.toString(array));
or if your array contains other arrays as elements:
System.out.println(Arrays.deepToString(array));
Videos
00:57
How can you print an array on the console? - Cracking the Java ...
04:22
Java Program to Print an Array | Java Array Printing Example - YouTube
05:27
Java Print Array using Methods | CodeGym University Course - YouTube
03:07
Printing An Array In A Table | Java For Beginners - YouTube
07:39
Java program to print array elements using Arrays class methods ...
04:36
Print all the values in an array in Java - YouTube
Programiz
programiz.com โบ java-programming โบ examples โบ print-array
Java Program to Print an Array
Become a certified Java programmer. Try Programiz PRO! ... public class Array { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; for (int element: array) { System.out.println(element); } } }
W3Schools
w3schools.com โบ java โบ java_arrays_loop.asp
Java Loop Through an Array
You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run. This example creates an array of strings and then uses a for loop to print each element, one by one:
W3Schools
w3schools.in โบ java โบ arrays
Java Arrays - W3Schools
public class Sample { public static void main(String args[]) { int[] newArray = new int[5]; // Initializing elements of array seperately for (int n = 0; n < newArray.length; n++) { newArray[n] = n; } System.out.println(newArray[2]); // Assigning 2nd element of array value } }
w3resource
w3resource.com โบ java-exercises โบ array โบ index.php
Java Array exercises: Array Exercises - w3resource
Example: Input : nums1= { 1, 2, -2, 3, 4, 5, 6 } nums2 = { 1, 2, 3, 4, 5, 6 } nums3 = { 1, 2, -3, 4, 5, 6 } Output: Does the said array contain a subarray with 0 sum: true Does the said array contain a subarray with 0 sum: false Does the said array contain a subarray with 0 sum: true ... Write a Java program to print all sub-arrays with 0 sum present in a given array of integers.
W3Schools
w3schools.com โบ java โบ java_variables_print.asp
Java Print/Display Variables
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
W3Schools
w3schools.com โบ java โบ java_output.asp
Java Output Values / Print Text
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
CodeGym
codegym.cc โบ java blog โบ java collections โบ how to print an array in java
How to Print an Array in Java
Months of the year are as follows: January February March April May June July August September October November December To reinforce what you learned, we suggest you watch a video lesson from our Java Course ยท Youโve already got the hang of printing simple arrays using methods like Arrays.toString(), but things get trickier with multi-dimensional arrays.
Published ย February 14, 2025
UpStack
upstackhq.com โบ upstack blog โบ software development
How To Print An Array In Java | Upstack
This is by far the easiest way to print or iterate through an array in any programming language; when asked to print an array as a programmer, the first thing you must do is start writing the loop. You can use for loop to iterate over the elements of the array. Below is a program that demonstrates the use of "for" loops in Java.
Stack Abuse
stackabuse.com โบ how-to-print-an-array-in-java
How to Print an Array in Java
February 24, 2023 - From there, we used streams so that we can quickly print array elements line by line. We've also covered how to convert an array to a list, which you can either just call or use the Java 8 Stream API for more granular control.
Java Code Geeks
javacodegeeks.com โบ home โบ core java
How to Print Array Contents in Java - Java Code Geeks
October 7, 2024 - System.out.println() โ This prints the string representation of the array. The code produces the following output in the IDE console: ... Multi-dimensional arrays, such as two-dimensional arrays, can be thought of as arrays of arrays.
Vultr Docs
docs.vultr.com โบ java โบ examples โบ print-an-array
Java Program to Print an Array | Vultr Docs
November 26, 2024 - Arrays can hold primitives (like int, char, etc.) or objects (like Strings or other Java objects). In this article, you will learn how to print arrays in Java using different methods. Explore practical examples that include using loops, Java 8 Streams, and the Arrays utility class.