一维数组转二维数组,旋转

  1. public class TestConvertArray {  
  2.   
  3.     public static void main(String[] args) {  
  4.           
  5.         int[] preOneDegree = {1,2,3,4,5,6};  
  6.         oneDegreeToTwoDegree(preOneDegree,3,2);  
  7.         twoDegreeConvertLeft90(preOneDegree,2,3);  
  8.         twoDegreeConvertRight90(preOneDegree,2,3);  
  9.     }  
  10.       
  11.     //一维转二维  
  12.     private static void oneDegreeToTwoDegree(int[] oneDegree,int row,int col){  
  13.           
  14.         int k = 0;  
  15.         int[][] twoDegree = new int[row][col];  
  16.         for(int i=0;i<row;i++){  
  17.             for(int j=0;j<col;j++){  
  18.                 twoDegree[i][j] = oneDegree[k];  
  19.                 k++;  
  20.             }  
  21.         }  
  22.         display(twoDegree,row,col);  
  23.     }  
  24.       
  25.     //二维一维  
  26.     private static void twoDegreeToOneDegree(int[][] twoDegree,int row,int col){  
  27.         int[] oneDegree = new int[row*col];  
  28.         int k = 0;  
  29.         for(int i=0;i<row;i++){  
  30.             for(int j=0;j<col;j++){  
  31.                 oneDegree[k] = twoDegree[i][j];  
  32.                 k++;  
  33.             }  
  34.         }  
  35.     }  
  36.       
  37.     //左转90度  
  38.     private static void twoDegreeConvertLeft90(int[] oneDegree,int row,int col){  
  39.           
  40.         int k = 0;  
  41.         int[][] convert90Degree = new int[row][col];  
  42.         for(int i=0;i<col;i++){  
  43.             for(int j=row-1;j>=0;j--){  
  44.                 convert90Degree[j][i] = oneDegree[k];  
  45.                 k++;  
  46.             }  
  47.         }  
  48.         display(convert90Degree, row, col);  
  49.     }  
  50.       
  51.     //右转90度  
  52.     private static void twoDegreeConvertRight90(int[] oneDegree,int row,int col){  
  53.           
  54.         int k = 0;  
  55.         int[][] convert90Degree = new int[row][col];  
  56.         for(int i=col-1;i>=0;i--){  
  57.             for(int j=row-1;j>=0;j--){  
  58.                 convert90Degree[j][i] = oneDegree[k];  
  59.                 k++;  
  60.             }  
  61.         }  
  62.         display(convert90Degree, row, col);  
  63.     }  
  64.       
  65.     //打印  
  66.     private static void display(int[][] arr,int row,int col){  
  67.         for(int i=0;i<row;i++){  
  68.             for(int j=0;j<col;j++){  
  69.                 System.out.print(arr[i][j]+" ");  
  70.             }  
  71.             System.out.println();  
  72.         }  
  73.         System.out.println();  
  74.     }  
  75.       
  76. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值