java 二维数组的应用

来源:互联网 发布:php pdo扩展 编辑:程序博客网 时间:2024/05/17 07:18


package com.base;
/**
 *
 * 二维数组的应用
 * 古诗由横着排版,改为竖着排版
 * @author kathleen
 *
 */
public class ArrayDemo {
 
 
 public static void main(String[] args) {
  //4行5列
  String [][] poetry = {
    {"床","前","明","月","光"},
    {"疑","是","地","上","霜"},
    {"举","头","望","明","月"},
    {"低","头","思","故","乡"}
  };
  
  //转为竖排后就变成了5行4列
  String [][] temp= new String[5][4];
  
  for(int i = 0 ; i < poetry.length; i++)
  {
   for(int j = 0 ; j< poetry[i].length; j++)
   {
    //4行5列  转换成5行4列时候,
    //前者的ij  和后者的ji位置上内容互换
    String tem = poetry[i][j];
    temp[j][i] = tem;
     System.out.print(tem);
   }
   System.out.println();
  }
  
  System.out.println("=====================");
  
  //打印互换后的结果
  for(int i = 0 ; i < temp.length; i++)
  {
   for(int j = 0 ; j< temp[i].length; j++)
   {
    
     System.out.print(temp[i][j]);
   }
   System.out.println();
  }
 }

}

0 0
原创粉丝点击