如何反向打印数组? [英] How can I print an array in reverse?

查看:137
本文介绍了如何反向打印数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数组代码,我需要反向打印。

  public class Array1D {
public static void main(String [] args){
int [] array = new int [3];
array [0] = 1;
array [1] = 2;
array [2] = 5; (int i = 0; i System.out.print(array [+ i +] =);
System.out.println(array [i] +);
}
System.out.println(matrix中的最后一个元素=+ array [array.length - 1]); //找到数组中的最后一个元素
System.out.println();
}
}


解决方案

反转您的for循环的方向。现在它是从0到长度,从长度到零计数$($ i pre pre pre pre $ length-1; i> = 0; i--)


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];
        array[0] = 1;
        array[1] = 2;
        array[2] = 5;
        for (int i = 0; i < array.length; i++) { //loop
            System.out.print("array["+i+"]=");
            System.out.println(array[i] + "   ");
        }
        System.out.println("the last element in the matrix = " + array[array.length - 1]); // finding the last element in the array
        System.out.println("   ");
    }
}

解决方案

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

这篇关于如何反向打印数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆