How to print array without using loop in java

How to print array in java without loop is frequently asked question in interview. We can print array without using any loop. We have different methods to do this. Firstly we will see example of how to print array in java using for loop .

public class PrintArrayUsingLoop {
	
	public static void main(String[] args) {
		int [] arr = {15,60,32,33,12};
		System.out.println("element of array are :");
		for (int i : arr) {
			System.out.print(i+"  ");
		}
		
	}
}

Output :

element of array are :
15  60  32  33  12

We have seen how to print array element using loop.

Print Array without using loop in Java

We have different method for print array in java without loop . Let’s discuss them

Method 1 : Print Array in java using Arrays’s toString() method

We will use Arrays’s method toString() to print array element without using loop or recursion in java

import java.util.Arrays;

public class PrintArray {
//print array without using loop in java
public static void main(String[] args) {

int [] arr = {15,60,32,33,12};
System.out.println(" Print array ="+ Arrays.toString(arr));

char [] crr = {'A','B','C','D','E','F'};

System.out.println(" Print Array ="+ Arrays.toString(crr));

}

}

Output:

Print array =[15, 60, 32, 33, 12]
Print Array =[A, B, C, D, E, F]
Method 2. Print Array Using Collection
In this way we will create ArrayList from array then print arraylist , Then we will print list without loop.
import java.util.ArrayList;
import java.util.Arrays;

public class PrintArrayUsingLoop {
	
	public static void main(String[] args) {
	    String [] brr ={"HTML","PHP","JAVA","C"};

	    ArrayList<String> arr= new ArrayList<String>(Arrays.asList(brr));

	    System.out.println("ArrayList Is ="+arr);  
	}
}
Output :
ArrayList Is =[HTML, PHP, JAVA, C]
In this example we have learned how to print list in java without loop also.

Print 2d Array in Java without loop

Now we will see how print 2d array in java without loop . We can print 2d array element using deepToString() method of Arrays.
import java.util.Arrays;

public class Print2dArrayWithOutUsingLoop {
	public static void main(String[] args) {
		int arr[][] = {{1}, {1, 2}, {1, 2, 3}};
		String str = Arrays.deepToString(arr);
		System.out.println(str);
	}
}
Output :
[[1], [1, 2], [1, 2, 3]]
In this tutorial we have learned how to print array with loop , how to print array without loop and how to print 2d array without using loop. You can see more java programming question for interview preparation .

2 comments

  1. import java.util.Scanner;
    class Demo
    {
    static int[] a={1,2,3,4,5,6,7,8};
    public static void main(String[] args)
    {

    Array(0);

    }
    public static void Array(int x)
    {
    System.out.println(a[x]);
    Array(++x);
    }

    }

  2. import java.util.Scanner;
    class Demo
    {
    static int[] a={1,2,3,4,5,6,7,8};
    public static void main(String[] args)
    {

    Array(0);

    }
    public static void Array(int x)
    {
    System.out.println(a[x]);
    Array(++x);
    }

    }

Leave a Reply

Your email address will not be published. Required fields are marked *

63 + = 69