4*4二维数组
{{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}}
打印顺序
4
3 8
2 7 12
1 6 11 16
5 10 15
9 14
13
两种实现方法,原理相同:
package com.edu.csu.test;
public class Test {
/**
* @param args
*/
public static void main(String[] args){
int[][] str = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
// int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
// arrayPrint(str);
array(str);
}
//方法一
private static void array(int[][] arr){
for(int y= 0; y<arr.length;y++){
int i = 0;
int j = arr.length-1-y;
for(int x = j;x < arr.length;i++,x++){
System.out.print(arr[i][x]+" ");
}
System.out.println();
}
for(int i = 1; i < arr.length; i++){
int x = 0;
int y = i;
for(int j = i;j < arr.length;j++,x++){
System.out.print(arr[j][x]+" ");
}
System.out.println();
}
}
//方法二:
private static void arrayPrint(int[][] str){
int i=0;
int j=str.length-1;
while(i<str.length){
for(int k=0;k<=i;k++){
System.out.print(str[k][j-i+k]+" ");
}
i++;
System.out.println();
}
i=1;
j=0;
while(i<str.length){
for(int k=i;k<str.length;k++){
System.out.print(str[k][j+k-i]+" ");
}
i++;
System.out.println();
}
}
}