How to convert an int array to String with toString method in Java
JavaArraysTostringJava Problem Overview
I am using trying to use the toString(int[])
method, but I think I am doing it wrong:
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#toString(int[])
My code:
int[] array = new int[lnr.getLineNumber() + 1];
int i = 0;
System.out.println(array.toString());
The output is:
[I@23fc4bec
Also I tried printing like this, but:
System.out.println(new String().toString(array)); // **error on next line**
The method toString() in the type String is not applicable for the arguments (int[])
I took this code out of bigger and more complex code, but I can add it if needed. But this should give general information.
I am looking for output, like in Oracle's documentation:
> The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space).
Java Solutions
Solution 1 - Java
What you want is the Arrays.toString(int[])
method:
import java.util.Arrays;
int[] array = new int[lnr.getLineNumber() + 1];
int i = 0;
..
System.out.println(Arrays.toString(array));
There is a static Arrays.toString
helper method for every different primitive java type; the one for int[]
says this:
> public static String toString(int[] a)
>
> Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"
). Adjacent elements are separated by the characters ", "
(a comma followed by a space). Elements are converted to strings as by String.valueOf(int)
. Returns "null"
if a
is null.
Solution 2 - Java
System.out.println(array.toString());
should be:
System.out.println(Arrays.toString(array));
Solution 3 - Java
Very much agreed with @Patrik M, but the thing with Arrays.toString is that it includes "[" and "]" and "," in the output. So I'll simply use a regex to remove them from outout like this
String strOfInts = Arrays.toString(intArray).replaceAll("\\[|\\]|,|\\s", "");
and now you have a String which can be parsed back to java.lang.Number
, for example,
long veryLongNumber = Long.parseLong(intStr);
Or you can use the java 8 streams, if you hate regex,
String strOfInts = Arrays
.stream(intArray)
.mapToObj(String::valueOf)
.reduce((a, b) -> a.concat(",").concat(b))
.get();
Solution 4 - Java
You can use java.util.Arrays:
String res = Arrays.toString(array);
System.out.println(res);
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Solution 5 - Java
The toString method on an array only prints out the memory address, which you are getting. You have to loop though the array and print out each item by itself
for(int i : array) {
System.println(i);
}
Solution 6 - Java
Using the utility I describe here, you can have a more control over the string representation you get for your array.
String[] s = { "hello", "world" };
RichIterable<String> r = RichIterable.from(s);
r.mkString(); // gives "hello, world"
r.mkString(" | "); // gives "hello | world"
r.mkString("< ", ", ", " >"); // gives "< hello, world >"
Solution 7 - Java
This function returns a array of int in the string form like "6097321041141011026"
private String IntArrayToString(byte[] array) {
String strRet="";
for(int i : array) {
strRet+=Integer.toString(i);
}
return strRet;
}
Solution 8 - Java
Here's an example of going from a list of strings, to a single string, back to a list of strings.
Compiling:
$ javac test.java
$ java test
Running:
Initial list:
"abc"
"def"
"ghi"
"jkl"
"mno"
As single string:
"[abc, def, ghi, jkl, mno]"
Reconstituted list:
"abc"
"def"
"ghi"
"jkl"
"mno"
Source code:
import java.util.*;
public class test {
public static void main(String[] args) {
List<String> listOfStrings= new ArrayList<>();
listOfStrings.add("abc");
listOfStrings.add("def");
listOfStrings.add("ghi");
listOfStrings.add("jkl");
listOfStrings.add("mno");
show("\nInitial list:", listOfStrings);
String singleString = listOfStrings.toString();
show("As single string:", singleString);
List<String> reconstitutedList = Arrays.asList(
singleString.substring(0, singleString.length() - 1)
.substring(1).split("[\\s,]+"));
show("Reconstituted list:", reconstitutedList);
}
public static void show(String title, Object operand) {
System.out.println(title + "\n");
if (operand instanceof String) {
System.out.println(" \"" + operand + "\"");
} else {
for (String string : (List<String>)operand)
System.out.println(" \"" + string + "\"");
}
System.out.println("\n");
}
}