1import java.util.Arrays;
2
3class Scratch{
4 public static void main(String[] args){
5 int[] arr = new int[3];
6 System.out.println( Arrays.toString( arr ));
7 //prints [0, 0, 0]
8 }
9}
1import java.util.Arrays;
2
3public class Array {
4
5 public static void main(String[] args) {
6 int[] array = {1, 2, 3, 4, 5};
7
8 System.out.println(Arrays.toString(array));
9 }
10}
1String[] array = new String[] {"John", "Mary", "Bob"};
2System.out.println(Arrays.toString(array)); // [John, Mary, Bob]
1import java.util.Arrays;
2//How to print Arrays in Java
3public class Printarray{
4
5 public static void main(String[] args){
6
7 String[] arr = new String[]{"Mumbai","Delhi","Kolkata","Chennai"};
8
9 //Using for loop;
10 for(int i=0; i<arr.length;i++){
11 System.out.println(arr[i]);
12 }
13
14 //Using for-each loop;
15 for(String city:arr){
16 System.out.println(city);
17 }
18
19 //Using Arrays.toString() method
20 System.out.println(Arrays.toString(arr));
21
22
23 //Using Arrays.deepToString() method its also converting multidimensional arrays to strings.
24
25 System.out.println(Arrays.deepToString(arr));
26
27 //Using Array as list method
28 System.out.println(Arrays.asList(arr));
29
30 }
31
32}
1public class Array {
2
3 public static void main(String[] args) {
4 int[] array = {1, 2, 3, 4, 5}; //Your array
5
6 for (int element: array) { //Advanced For Loop (For each)
7 System.out.println(element); //print each element of
8 //the array for each iteration
9 }
10 }
11}