In JAVA 5 or above, you can use Array.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. As you can see in the example below that object[] version calls .toString() on each object in the array. The output in the examples is decorated as you asked.
Here are some examples:
1. For Simple Array:
Input:
String[] array = new String[] {"Abhi", "Kabhi", "Sabhi"};
System.out.println(Arrays.toString(array));
Output:
[Abhi, Kabhi, Sbhi]
2. For Nested Array:
Input:
String[][] deepArray = new String[][] {{"Abhi", "Kabhi"}, {"Nbhi", "Sbhi"}};
System.out.println(Arrays.toString(deepArray));
//output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
System.out.println(Arrays.deepToString(deepArray));
Output:
[[Abhi, Kabhi], [Nbhi, Sbhi]]
3. For int Array:
Input:
int[] intArray = { 8, 7, 6, 2, 4 };
System.out.println(Arrays.toString(intArray));
Output:
[8, 7, 6, 2, 4 ]
4. For double Array:
Input:
double[] doubleArray = { 8.0, 7.0, 6.0, 2.0, 4.0 };
System.out.println(Arrays.toString(doubleArray));
Output:
[8.0,7.0, 6.0, 2.0, 4.0 ]
5. You can also use loop:
Input:
public class Array
{
public static void main(String[] args) ]
{
int[] array = {2, 3, 4, 5, 6};
for (int element: array)
{
System.out.println(element);
}
}
}
Output:
2
3
4
5
6