数组之逆序输出也是数组操作中较重要的一环,下面来看看该如何操作。
例1:逆序输出一个一维数组中元素
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { static String[] sz = { "a" , "b" , "c" , "d" , "e" , "f" /* ... */ }; static void showTable(int n) { for (int i = 1; i <= n; i++) { for (int j = 0; j <= n - i; j++) { System.out.print(" "); } for (int j = i - 1; j >= 0; j--) { System.out.print(sz[j]); } System.out.println(); } } public static void main(String[] args) throws java.lang.Exception { // your code goes here showTable(5); } }
例2:依次输入五句话,并逆序输出
import java.util.Scanner; public class A01 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("请输入5句话:"); String[] names = new String[5]; for (int i = 0; i < names.length; i++) { System.out.print("第" + (i + 1) + "句话:"); names[i] = input.next(); } System.out.println("逆序输出的5句话为:"); for (int i = names.length - 1; i >= 0; i--) { System.out.println(names[i]); } } }
以上就是本篇文章的所有内容,如果需要了解更多java编程常见问题及答案就请持续关注本站吧。
推荐阅读: