/**
成都創(chuàng)新互聯(lián)公司憑借專業(yè)的設(shè)計團隊扎實的技術(shù)支持、優(yōu)質(zhì)高效的服務意識和豐厚的資源優(yōu)勢,提供專業(yè)的網(wǎng)站策劃、成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)站優(yōu)化、軟件開發(fā)、網(wǎng)站改版等服務,在成都10余年的網(wǎng)站建設(shè)設(shè)計經(jīng)驗,為成都上1000+中小型企業(yè)策劃設(shè)計了網(wǎng)站。
* @author 熙熙
* @2011-4-20
*/
public class JuZhenZhuanZhi {
/**
* @param args
*/
public static void main(String[] args) {
int a[][]={
{11,12,13}
,{21,22,23}
,{31,32,33}
};
System.out.println("轉(zhuǎn)置前的矩陣:");
print(a);
zhuanZhi(a);
System.out.println("轉(zhuǎn)置后的矩陣:");
print(a);
}
public static void zhuanZhi(int a[][]){
for(int i=0;ia.length;i++){
for(int j=i+1;ja[i].length;j++){
int tem = a[i][j];
a[i][j] = a[j][i];
a[j][i] = tem;
}
}
}
public static void print(int a[][]){
for(int i=0;ia.length;i++){
int j=0;
for(;ja[i].length-1;j++){
System.out.print(a[i][j]+",");
}
System.out.println(a[i][j]);
}
}
}
借花獻佛
/**
*?實現(xiàn)二維數(shù)組的轉(zhuǎn)置
*?@author?HAN
*
*/
public?class?transposition_Arrays2D_ch6_4?{
final?static?double?PI=3.1415;
public?static?void?main(String[]?args)?{
/*StaticTest?st1=new?StaticTest();
StaticTest?st2=new?StaticTest();
st1.method2("HAN");*/
/*****定義要用于轉(zhuǎn)置的二維數(shù)組*******/
int?arr2D[][]={{1,2,3},{4,5,6},{7,8,9}};
/*****構(gòu)造結(jié)果新二維數(shù)組用于存放轉(zhuǎn)置結(jié)果*******/
/*定義結(jié)果數(shù)組變量,注意?一定要先開辟一個內(nèi)存,
否則只是地址傳遞,也就是說兩個數(shù)組名實際上指向的是同一塊內(nèi)存*/
//而構(gòu)造二維數(shù)組可以為維度來進行,不一定是一個矩陣,即每一行的長度不一定相同
int?result_arr[][]=new?int[arr2D.length][];//先實現(xiàn)第一維
for(int?i=0?;?iarr2D.length;i++){?//再實現(xiàn)第二維
result_arr[i]=new?int[arr2D[i].length];
}
// int?result_arr[][]=Arrays.copyOf(arr2D,?arr2D.length);
//上面這個命令行行不通!
/*****輸出用于轉(zhuǎn)置的二維數(shù)組*******/
for?(int?x[]:arr2D){
for(int?e:x){
System.out.print(e+"?");
}
System.out.println();
}
System.out.println();
/*******進行元素倒置******/
for(int?i=0?;?iarr2D.length;i++){
for(int?j=0;?jarr2D[i].length;j++){
result_arr[j][i]=arr2D[i][j];?//轉(zhuǎn)置核心
}
}
/*****show?the?result?in?the?result?matrix*******/
for?(int?x[]:result_arr){
for(int?e:x){
System.out.print(e+"?");
}
System.out.println();
}
}
}
//import?java.util.Arrays;
//public?class?transposition_Arrays2D?{
//
// public?static?void?main(String[]?args)?{
// int?arr2D[][]={{1,2,3},{4,5,6},{7,8,9}};
// /*定義結(jié)果數(shù)組變量,注意?一定要先開辟一個內(nèi)存,
// 否則只是地址傳遞,也就是說兩個數(shù)組名實際上指向的是同一塊內(nèi)存*/
// int?result_arr[][]=new?int[arr2D.length][];
// for(int?i=0?;?iarr2D.length;i++){
// result_arr[i]=new?int[arr2D[i].length];
// }
//
//????????//?進行元素倒置
//????????for(int?i=0?;?iarr2D.length;i++){
//???????? for(int?j=0;?jarr2D[i].length;j++){
//???????? result_arr[j][i]=arr2D[i][j];
//???????? }
//????????}
//????????
//????????//?show?the?result?in?matrix
//????????for?(int?x[]:result_arr){
//???????? for(int?e:x){
//???????? System.out.print(e);
//???????? }
//???????? System.out.println();
//????????}
//????????
// }
//
//}
int a[][] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
int b[][] = new int [4][7];
for (int i = 0; i 4; i++) {
for (int j = 0; j 7; j++) {
b[i][j] = a[j][i];
}
}
System.out.println("矩陣a:");
for (int i = 0; i 7; i++) {
for (int j = 0; j 4; j++) {
if (j 3) {
System.out.print(a[i][j] + " ");
} else {
System.out.println(a[i][j] + " ");
}
}
}
System.out.println("矩陣a轉(zhuǎn)置:");
for (int i = 0; i 4; i++) {
for (int j = 0; j 7; j++) {
if (j 6) {
System.out.print(b[i][j] + " ");
} else {
System.out.println(b[i][j] + " ");
}
}
}
代碼寫得爛不要噴我,我想解決思路就是這樣吧!