Convert an int array to String in Java
This post will discuss how to convert an int array to a String in Java.
1. Using Stream API
You can Java 8 Streams to easily convert an int array to a string. The idea is to get an IntStream for the int array and map each element of the stream to a String object. Then perform a reduction operation on stream elements using the Stream.reduce() method that returns an Optional describing the reduced object. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] intArray = { 1, 2, 3, 4, 5 }; String str = Arrays.stream(intArray) .mapToObj(String::valueOf) .reduce((x, y) -> x + ", " + y) .get(); System.out.println(str); } } |
Output:
1, 2, 3, 4, 5
Here’s a version that works with an Integer array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; public class Main { public static void main(String[] args) { Integer[] intArray = { 1, 2, 3, 4, 5 }; String str = Arrays.stream(intArray) .map(String::valueOf) .reduce((x, y) -> x + ", " + y) .get(); System.out.println(str); } } |
Output:
1, 2, 3, 4, 5
2. Using Arrays.toString() method
If you just need to print the string representation of an integer array, you can do so using the Arrays.toString() method. It returns the array’s elements, enclosed in square brackets [] where the adjacent elements are separated by ", ".
|
1 2 3 4 5 6 7 8 9 10 |
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] intArray = { 1, 2, 3, 4, 5 }; System.out.println(Arrays.toString(intArray)); } } |
Output:
[1, 2, 3, 4, 5]
If you need to remove [ and ] from the output, you can use the replaceAll() method on the string representation.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] intArray = { 1, 2, 3, 4, 5 }; String str = Arrays.toString(intArray).replaceAll("\\[|]", ""); System.out.println(str); } } |
Output:
1, 2, 3, 4, 5
3. Using Custom Routine
Finally, you can write your custom method to convert an int array to a String. The following solution creates a new string and concatenates each array element to it, separated by ", ".
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class Main { private static String getString(int[] intArray) { if (intArray == null || intArray.length == 0) { return ""; } String str = String.valueOf(intArray[0]); for (int i = 1; i < intArray.length; i++) { str = str.concat(", ").concat(String.valueOf(intArray[i])); } return str; } public static void main(String[] args) { int[] intArray = { 1, 2, 3, 4, 5 }; String str = getString(intArray); System.out.println(str); } } |
Output:
1, 2, 3, 4, 5
That’s all about converting an int array to a String in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)