2014-12-23 8 views
1

에 따른 대칭 행렬의 요소를 교환 방법 가지고 2dimantional 배열을 생성하고 정상적으로 작동하지만 이제 역방향의 마지막 행 될 첫 번째 행을 확인하려면 코드 . 당신이 열이있는 경우오른쪽 대각선

1 
2 
3 
4 

이 같은 새로운 배열에 와서해야처럼

는 :

1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16 

을 :

4 3 2 1 

내가 가지고있는 기질이있다 새로운 행렬은 다음과 같이해야합니다 :

012 379,

이 내 코드입니다 :

package test5; 

public class test5 { 

    public static void main(String[] args) { 
     int[][] nums = new int[4][4]; 

     nums[0][0] = 1; 
     nums[0][1] = 2; 
     nums[0][2] = 3; 
     nums[0][3] = 4; 

     nums[1][0] = 5; 
     nums[1][1] = 6; 
     nums[1][2] = 7; 
     nums[1][3] = 8; 

     nums[2][0] = 9; 
     nums[2][1] = 10; 
     nums[2][2] = 11; 
     nums[2][3] = 12; 

     nums[3][0] = 13; 
     nums[3][1] = 14; 
     nums[3][2] = 15; 
     nums[3][3] = 16; 

     for (int i = 0; i < nums.length; i++) { 
      for (int j = 0; j < nums.length; j++) { 

       System.out.print(nums[i][j] + " "); 
      } 
      System.out.println(); 
     } 
    } 
} 

그래서 누군가가 나를 도울 수 ???

+0

을 작업 .... –

답변

0

당신이 원하는대로 할 필요가있는 경우,이 같은 시도 : 더 매트릭스를 할 경우 http://math.nist.gov/javanumerics/jama/ : 당신은 한 번 봐 고려해 볼 수 있습니다

public class MatrixTranspose { 
     static int m1[][] = new int[][]{{1, 2, 3, 4, 5}, {5, 6, 7, 8, 9}, {9, 10, 11, 12, 13}, {13, 14, 15, 16, 17}}; 
     public static String toString(int[][] m) { 
      StringBuilder text = new StringBuilder(); 
      for (int row = 0; row < m.length; row++) { 
       int r[] = m[row]; 
       for (int col = 0; col < r.length; col++) { 
        if (col > 0) text.append(", "); 
        text.append(r[col]); 
       } 
       text.append("\n"); 
      } 
      return text.toString(); 
     } 

     public static int[][] transpose(int[][] m) { 
      int rows = m.length; 
      int cols = m[0].length; 

      int t[][] = new int[cols][]; // first create the empty transpose matrix 
      for (int trow = 0; trow < cols; trow++) { 
       t[trow] = new int[rows]; 
      } 

      for (int row = 0; row < rows; row++) { 
       for (int col = 0; col < cols; col++) { 
        int tcol = rows-row-1; // transposed tcol is inverted row 
        int trow = cols-col-1; // transposed trow is inverted col 
        t[trow][tcol] = m[row][col]; 
       } 
      } 
      return t; 
     } 

     public static void main(String...params) { 
      System.out.println(toString(m1)); 
      System.out.println("--"); 
      System.out.println(toString(transpose(m1))); 
     } 
    } 
+0

당신의 도움을 주셔서 감사합니다 당신은 더 자세히 조바꿈 함수를 설명 할 수 있습니까? –