Wednesday 25 November 2015

How to print array without using loop in java

How to print array without using loop in java is frequently asked question in interview. We can print array without using any loop. For this purpose we will use Arrays's method toString().

package com.javaproficiency;

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]

3 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);
    }



    }

    ReplyDelete
  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);
    }



    }

    ReplyDelete