In this article, we show how to write a Java program to convert array to string. For instance, if there is an array of customer information (first name, middle, and last name), we can convert it to a string with the full name, which is easy to maintain and meaningful.
Java Program to convert Array to String using Arrays.toString()
In the following program, we use the built-in Arrays.toString() method to convert the given array to a string. The toString() method accepts the array and returns the string representation of all the array items.
TIP: We can use Arrays.toString() on 2D arrays. However, if there is a for loop to iterate over the array, we can use toString() on nested array elements to convert them to a string.
import java.util.Arrays;
public class example {
public static void main(String[] args) {
int[] a = {1, 3, 6, 9, 12, 15 };
System.out.println(Arrays.toString(a));
}
}
[1, 3, 6, 9, 12, 15]
Java Program to convert Array to String using join() method
In the following program, we use the built-in String join() method to convert the given array to a string. The Java String.join() method accepts the first argument as the separator and the second one as the array. It joins all array elements into a single string, and each element is separated by the first argument.
public class example {
public static void main(String[] args) {
String[] a = {"Bali", "Kerala", "Paris", "Sydney", "Amsterdam"};
String dest = String.join("-", a);
System.out.println(dest);
}
}
Bali-Kerala-Paris-Sydney-Amsterdam
Similarly, if it is a character array, we can use the Java String valueOf function. Please refer to the Java Character Array to String article.
public class example {
public static void main(String[] args) {
char[] a = { 'B', 'K', 'P', 'S', 'A' };
String dest = String.valueOf(a);
System.out.println(dest);
}
}
BKPSA
Java Program to convert Array to String using for loop
In this program, we use a Java for loop to iterate over the array elements. In each iteration, we use the arithmetic + operator to join the element to an already declared empty string. Refer to Java Arithmetic operators.
TIP: If you don’t want empty spaces between each array element, remove ” ” from the code below.
public class example {
public static void main(String[] args) {
String[] a = { "Bali", "Kerala", "Paris", "Sydney", "Amsterdam" };
String dest = "";
for (String fav : a) {
dest += fav + " ";
}
System.out.println(dest);
}
}
Bali Kerala Paris Sydney Amsterdam
Program to convert Array to String using StringBuilder
In this program, we declare a StringBuilder object. The for loop iterates over the integer array, and the append() method adds each array element to the StringBuilder. Next, convert the StringBuilder to a string.
It may look like a long process, but StringBuilder has many built-in methods to perform various data transformations. We can do that and then convert back to a string.
public class example {
public static void main(String[] args) {
int[] a = { 1, 3, 6, 9, 12, 15 };
StringBuilder sb = new StringBuilder();
for (int i = 0; i < a.length; i++) {
sb.append(a[i] + " ");
}
String s = sb.toString();
System.out.println(s);
}
}
1 3 6 9 12 15
Java Program to convert 2D Array to String
In the program, we use Arrays.deepToString() method to convert the given two-dimensional array to a string.
import java.util.Arrays;
public class example {
public static void main(String[] args) {
int[][] mat = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
System.out.println(Arrays.deepToString(mat));
}
}
[[1, 2], [3, 4], [5, 6]]
If you want to use Arrays.toString() to convert a 2D array to a string, use the technique below.
import java.util.Arrays;
public class example {
public static void main(String[] args) {
int[][] mat = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
for (int[] n : mat) {
System.out.println(Arrays.toString(n));
}
}
}
[1, 2]
[3, 4]
[5, 6]