标签:java
第4天
-------------------------
数组的转置:
1、前提条件是二维数组是正方的,即n行n列。
/**
* 二维数据的转置
*/
public class ArrayTransDemo {
public static void main(String [] args){
String[][] arr = {{"a","b","c","d","e"},{"f","g","h","i","j"},{"k","l","m","n","o"},{"p","q","r","s","t"},{"u","v","w","x","y"}};
print(arr);
System.out.println("----转置后--------------------------");
trans(arr);
print(arr);
}
private static String [][] trans(String [][] arr){
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if(j > i){
String temp = "";
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
return arr;
}
private static void print(String [][] arr){
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
/**
* 三维数组的打印
*/
public class Array3PrintDemo {
public static void main(String[] args) {
int [][][] arr = {{{1,2,3},{4,5,6},{7,8,9}},{{10,11,12},{13,14,15},{16,17,18}},{{19,20,21},{22,23,24},{25,26,27}}};
print(arr);
}
private static void print(int [][][] arr){
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
for (int k = 0; k < arr.length; k++) {
System.out.print(arr[j][i][k] +"\t");
}
System.out.print("|\t");
}
System.out.println();
}
}
}
标签:java
原文地址:http://11181682.blog.51cto.com/11171682/1770283