How can I print an array in reverse?

前端 未结 3 670
半阙折子戏
半阙折子戏 2021-01-29 16:51

This is my array code and I need to print it in reverse.

public class Array1D {
    public static void main(String[] args) {
        int[] array = new int[3];
           


        
相关标签:
3条回答
  • 2021-01-29 17:04

    If you really want to reverse the array, use:

    ArrayUtils.reverse(int[] array)
    

    But I suggest using a reversed loop.

    0 讨论(0)
  • 2021-01-29 17:16

    Just reverse the direction of your for loop. Right now it is counting from 0 to the length, have it count from the length to zero

    for (int i = array.length-1; i >= 0; i--)
    
    0 讨论(0)
  • 2021-01-29 17:17

    try like this (sample pseudocode) :

    for (i=array length-1;i=>0;i--){
    print(i);
    }
    
    0 讨论(0)
提交回复
热议问题