Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print Array content as String in Java?

How can the Array content be converted to a String in Java?

Example:

int[] myArray = {1,2,3};

The output has to be:

"123"

Arrays.toString(myArray) is returning:

"[1, 2, 3]"

and myArray.toString(), returns:

[I@12a3a380

So none of them works. Is there a function for this?

This question might look similar to (this), but is actually different. I am literally asking for a String made of all the array entries.

like image 897
Giacomo Avatar asked Sep 07 '18 13:09

Giacomo


1 Answers

String joined = Arrays.stream(myArray)
            .mapToObj(String::valueOf)
            .collect(Collectors.joining(""));

System.out.println(joined);   
like image 110
Eugene Avatar answered Oct 06 '22 01:10

Eugene