Array to String
In this chapter you will learn:
Convert array value to a String
The following methods returns a string representation of the contents of the specified array. Those methods are often used during debug.
static String toString(boolean[] a)
static String toString(byte[] a)
static String toString(char[] a)
static String toString(double[] a)
static String toString(float[] a)
static String toString(int[] a)
static String toString(long[] a)
static String toString(Object[] a)
static String toString(short[] a)
The following code demonstrates how to use toString
method
to convert array to String.
import java.util.Arrays;
// j a v a 2 s. c om
public class Main {
public static void main(String[] args) {
byte[] b1 = new byte[] { 3, 2, 5, 4, 1 };
System.out.println(Arrays.toString(b1));
Arrays.sort(b1);
System.out.println(Arrays.toString(b1));
byte[] b2 = new byte[] { 5, 2, 3, 1, 4 };
Arrays.sort(b2, 1, 4);
System.out.println(Arrays.toString(b2));
}
}
The output:
Deep to multi-dimensional string
static String deepToString(Object[] a)
returns a string representation of the "deep contents" of the specified array.
import java.util.Arrays;
//from j a v a2 s . com
public class Main {
public static void main(String args[]) {
String s[] = {"a", "b", "c", "d"};
double d [][]= {
{0.50, 0.20, 0.20, 0.30},
{0.50, 1.10, 0.50, 0.80},
{0.50, 0.70, 0.40},
{0.50, 0.70},
{0.50},
};
System.out.println(Arrays.toString(s));
System.out.println(Arrays.deepToString(d));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Array