How to convert an int array to String with toString method in Java

576,878

Solution 1

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

System.out.println(array.toString());

should be:

System.out.println(Arrays.toString(array));

Solution 3

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

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

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);
}
Share:
576,878

Related videos on Youtube

Jaanus
Author by

Jaanus

Doing C#, Java 50-50. SOreadytohelp

Updated on July 08, 2022

Comments

  • Jaanus
    Jaanus almost 2 years

    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).

  • clearlight
    clearlight over 8 years
    What's the easiest way to go from "[x, y, z]" back to an array or List?
  • Sbodd
    Sbodd over 8 years
    @nerdistcolony There's nothing as clean as Arrays.toString to go the other way - but see stackoverflow.com/questions/456367/… for some suggestions.
  • clearlight
    clearlight over 8 years
    @Sbodd - I came up with an example based on an ArrayList and posted the answer. It was specific to a problem I needed to solve for an app I'm porting, but might save someone some time.
  • ironstone13
    ironstone13 about 8 years
    It's better to use StringBuilder rather than direct string concatenation - for performance reasons
  • Roberto
    Roberto about 6 years
    Nobody asked for this.
  • clearlight
    clearlight about 6 years
    @Roberto It uses related technology (Array methods), and I provided it to supplement the int related answer for people working with array conversions even though it show strings because I knew there were already int related solutions.. No reason to downvote it and being smug about it. Maybe try doing some good around here before trying to trash other people.
  • clearlight
    clearlight about 6 years
    No you're wrong. First sentence clarifies the purpose, and it is related enough. Contribute something useful before thinking about crapping on other users. You've done nothing other than make things worse for others.
  • Ahamadullah Saikat
    Ahamadullah Saikat over 5 years
    how to retrieve int[] array from this Output.
  • sebyku
    sebyku over 4 years
    The regex one is really nasty ...