What's the simplest way to print a Java array?
In Java, arrays don't override toString(), so if you try to print one directly, you get the className+'@'+ the hex of the hashCode of the array, as defined by Object.toString():int[] intArray = new...
View ArticleAnswer by Esko for What's the simplest way to print a Java array?
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...
View ArticleAnswer by Limbic System for What's the simplest way to print a Java array?
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...
View ArticleAnswer by Ross for What's the simplest way to print a Java array?
If you're using Java 1.4, you can instead do:System.out.println(Arrays.asList(array));(This works in 1.5+ too, of course.)
View ArticleAnswer by Russ Bateman for What's the simplest way to print a Java array?
This is nice to know, however, as for "always check the standard libraries first" I'd never have stumbled upon the trick of Arrays.toString( myarray )--since I was concentrating on the type of myarray...
View ArticleAnswer by somedude for What's the simplest way to print a Java array?
for(int n: someArray) { System.out.println(n+"");}
View ArticleAnswer by Rhyous for What's the simplest way to print a Java array?
Arrays.deepToString(arr) only prints on one line. int[][] table = new int[2][2];To actually get a table to print as a two dimensional table, I had to do this:...
View ArticleAnswer by Andrew_Dublin for What's the simplest way to print a Java array?
Using regular for loop is the simplest way of printing array in my opinion.Here you have a sample code based on your intArrayfor (int i = 0; i < intArray.length; i++) { System.out.print(intArray[i]...
View ArticleAnswer by Eric Baker for What's the simplest way to print a Java array?
In JDK1.8 you can use aggregate operations and a lambda expression:String[] strArray = new String[] {"John", "Mary", "Bob"};// #1Arrays.asList(strArray).stream().forEach(s ->...
View ArticleAnswer by Jean Logeart for What's the simplest way to print a Java array?
To add to all the answers, printing the object as a JSON string is also an option.Using Jackson:ObjectWriter ow = new...
View ArticleAnswer by Roam for What's the simplest way to print a Java array?
There's one additional way if your array is of type char[]:char A[] = {'a', 'b', 'c'}; System.out.println(A); // no other argumentsprints abc
View ArticleAnswer by user3369011 for What's the simplest way to print a Java array?
public class printer { public static void main(String[] args) { String a[] = new String[4]; Scanner sc = new Scanner(System.in); System.out.println("enter the data"); for (int i = 0; i < 4; i++) {...
View ArticleAnswer by Mohamed Idris for What's the simplest way to print a Java array?
A simplified shortcut I've tried is this: int x[] = {1,2,3}; String printableText = Arrays.toString(x).replaceAll("[\\[\\]]", "").replaceAll(", ", "\n"); System.out.println(printableText);It will...
View ArticleAnswer by akhil_mittal for What's the simplest way to print a Java array?
Prior to Java 8We could have used Arrays.toString(array) to print one dimensional array and Arrays.deepToString(array) for multi-dimensional arrays. Java 8Now we have got the option of Stream and...
View ArticleAnswer by Haim Raman for What's the simplest way to print a Java array?
Using org.apache.commons.lang3.StringUtils.join(*) methods can be an optionFor example:String[] strArray = new String[] { "John", "Mary", "Bob" };String arrayAsCSV = StringUtils.join(strArray, " ,...
View ArticleAnswer by laylaylom for What's the simplest way to print a Java array?
Starting with Java 8, one could also take advantage of the join() method provided by the String class to print out array elements, without the brackets, and separated by a delimiter of choice (which is...
View ArticleAnswer by YoYo for What's the simplest way to print a Java array?
Arrays.toStringAs a direct answer, the solution provided by several, including @Esko, using the Arrays.toString and Arrays.deepToString methods, is simply the best.Java 8 - Stream.collect(joining()),...
View ArticleAnswer by Debosmit Ray for What's the simplest way to print a Java array?
I came across this post in Vanilla #Java recently. It's not very convenient writing Arrays.toString(arr);, then importing java.util.Arrays; all the time.Please note, this is not a permanent fix by any...
View ArticleAnswer by Greesh Kumar for What's the simplest way to print a Java array?
It should always work whichever JDK version you use:System.out.println(Arrays.asList(array));It will work if the Array contains Objects. If the Array contains primitive types, you can use wrapper...
View ArticleAnswer by suatCoskun for What's the simplest way to print a Java array?
In java 8 it is easy. there are two keywordsstream: Arrays.stream(intArray).forEachmethod reference: ::printlnint[] intArray = new int[] {1, 2, 3, 4,...
View Article