简体   繁体   中英

How to print array without using loop and recursion In java

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 .

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

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

使用 charAt() 方法打印数组的元素

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM