Answer by Mahozad for What's the simplest way to print a Java array?
For Android developers ending up here, this is for Kotlin:println(myArray.joinToString())ORprintln(myArray.joinToString(separator = "|"))
View ArticleAnswer by Shinu Mathew for What's the simplest way to print a Java array?
Use the Arrays class. It has multiple utility methods and its toString() is overriden to display array elements in a human readable way.Arrays.toString(arr)
View ArticleAnswer by Nikhil S Marathe for What's the simplest way to print a Java array?
It is very simple way to print array without using any loop in JAVA.-> For, Single or simple array: int[] array = new int[]{1, 2, 3, 4, 5, 6}; System.out.println(Arrays.toString(array));The Output :...
View ArticleAnswer by jkwli for What's the simplest way to print a Java array?
By using the java.util.Arrays:String mRole = "M_XXX_ABC"; System.out.println(Arrays.asList(mRole.split("_")).toString());output: [M, XXX, ABC]
View ArticleAnswer by Anis KCHAOU for What's the simplest way to print a Java array?
If you are using Java 11import java.util.Arrays;public class HelloWorld{ public static void main(String []args){ String[] array = { "John", "Mahta", "Sara" };...
View ArticleAnswer by Chiara Tumminelli for What's the simplest way to print a Java array?
Here a possible printing function: public static void printArray (int [] array){ System.out.print("{ "); for (int i = 0; i < array.length; i++){ System.out.print("[" + array[i] +"] "); }...
View ArticleAnswer by Anubhav Mishra 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 Chen Jin for What's the simplest way to print a Java array?
toString is a way to convert an array to string.Also, you can use:for (int i = 0; i < myArray.length; i++){System.out.println(myArray[i] +"");}This for loop will enable you to print each value of...
View ArticleAnswer by Pradip Karki for What's the simplest way to print a Java array?
If using Commons.Lang library, we could do:ArrayUtils.toString(array)int[] intArray = new int[] {1, 2, 3, 4, 5};String[] strArray = new String[] {"John", "Mary",...
View ArticleAnswer by duyuanchao for What's the simplest way to print a Java array?
if you are running jdk 8. public static void print(int[] array) { StringJoiner joiner = new StringJoiner(",", "[", "]"); Arrays.stream(array).forEach(element -> joiner.add(element +""));...
View ArticleAnswer by Gayan Sampath for What's the simplest way to print a Java array?
There are several ways to print an array elements.First of all, I'll explain that, what is an array?..Array is a simple data structure for storing data..When you define an array , Allocate set of...
View ArticleAnswer by Peter Lawrey for What's the simplest way to print a Java array?
This is marked as a duplicate for printing a byte[]. Note: for a byte array there are additional methods which may be appropriate.You can print it as a String if it contains ISO-8859-1 chars.String s =...
View ArticleAnswer by Mehdi for What's the simplest way to print a Java array?
In java 8 :Arrays.stream(myArray).forEach(System.out::println);
View ArticleAnswer by fjnk for What's the simplest way to print a Java array?
// array of primitives:int[] intArray = new int[] {1, 2, 3, 4, 5};System.out.println(Arrays.toString(intArray));output: [1, 2, 3, 4, 5]// array of object references:String[] strArray = new String[]...
View ArticleAnswer by Ravi Patel for What's the simplest way to print a Java array?
There Are Following way to print Array // 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt));// 2 for loop() for (int number : arrayInt) {...
View ArticleAnswer by hasham.98 for What's the simplest way to print a Java array?
For-each loop can also be used to print elements of array:int array[] = {1, 2, 3, 4, 5};for (int i:array) System.out.println(i);
View ArticleAnswer by Aftab for What's the simplest way to print a Java array?
Different Ways to Print Arrays in Java:Simple Way List<String> list = new ArrayList<String>();list.add("One");list.add("Two");list.add("Three");list.add("Four");// Print the list in...
View ArticleAnswer by Dylan Black for What's the simplest way to print a Java array?
You could loop through the array, printing out each item, as you loop. For example:String[] items = {"item 1", "item 2", "item 3"};for(int i = 0; i < items.length; i++) {...
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 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 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 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 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 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 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 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 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 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 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 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 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 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 somedude for What's the simplest way to print a Java array?
for(int n: someArray) { System.out.println(n+"");}
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 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 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 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 ArticleWhat'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 Article