4

In my application there is need to print values of array. I can not use any loop or recursion and want to print all values from http response. There is any way to print java array without using loop or recursion. For Example I have array int [] ={102,202,..12}. now i want to print values as

102,202 .. 12 . Order maintain is not necessary .

  • Integer[] myArray = {2,3,4}; System.out.println(Arrays.asList(myArray)); It is just a trick...but not excatly the solution – Naruto Dec 05 '15 at 05:49
  • Look at this other [question](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Tom Dec 05 '15 at 05:50
  • At the end a loop will be used, it may just be hidden behind a function call so that it is not directly visible in the program. – Henry Dec 05 '15 at 06:12

5 Answers5

6
Method 1:

We can print array without using loop or recursion as

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

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

Output: Print Array =[A, B, C, D, E, F] 


Method 2: Firstly we make arraylist from array and then print it .

    String [] brr ={"HTML","PHP","JAVA","C"};

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

    System.out.println("ArrayList Is ="+arr);    

Source : Print array without using loop/recursion

Anuj Dhiman
  • 2,060
  • 1
  • 20
  • 44
1

Please check this answer.

public class Test {

    public static void main(String[] args) {
        Test64Numbers();
        Test32Numbers();
        Test4Numbers();
    }

    private static int currentNumber = 0;

    private static void Test1Number() { System.out.println(++currentNumber); }
    private static void Test2Numbers() { Test1Number(); Test1Number(); }
    private static void Test4Numbers() { Test2Numbers(); Test2Numbers(); }
    private static void Test8Numbers() { Test4Numbers(); Test4Numbers(); }
    private static void Test16Numbers() { Test8Numbers(); Test8Numbers(); }
    private static void Test32Numbers() { Test16Numbers(); Test16Numbers(); }
    private static void Test64Numbers() { Test32Numbers(); Test32Numbers(); }
}
amit prasad
  • 504
  • 3
  • 15
0

use the charAt() method to print the elements of the array

  • Welcome to SO. Can you give a code example how your suggestion would work for the problem of the original poster? – m00am Dec 05 '15 at 06:33
0

We can use this function to print any dimension array. This also formats into a matrix kind when printing, this improves readability.

public static void printArray(Object[] R){
    System.out.println(Arrays.deepToString(R).replaceAll("],", "]," + System.getProperty("line.separator")));
}
Tokala Sai Teja
  • 580
  • 6
  • 9
0

We can print array elements using recursion also. see the code below.

// Pass array and x = 0 to the method

private static void printArray(int[] arr, int x) {
    if (x >= arr.length)
    {
        // Return, if x is greater or equal to size of Array.
        return;
    }

    // Else print element and recursively call for next element
    System.out.print(arr[x] + “ “);
    printArray(arr, ++x);
}
Andrew Regan
  • 4,997
  • 6
  • 37
  • 68
B Sangappa
  • 524
  • 4
  • 8