- public class TestConvertArray {
- public static void main(String[] args) {
- int[] preOneDegree = {1,2,3,4,5,6};
- oneDegreeToTwoDegree(preOneDegree,3,2);
- twoDegreeConvertLeft90(preOneDegree,2,3);
- twoDegreeConvertRight90(preOneDegree,2,3);
- }
- //一维转二维
- private static void oneDegreeToTwoDegree(int[] oneDegree,int row,int col){
- int k = 0;
- int[][] twoDegree = new int[row][col];
- for(int i=0;i<row;i++){
- for(int j=0;j<col;j++){
- twoDegree[i][j] = oneDegree[k];
- k++;
- }
- }
- display(twoDegree,row,col);
- }
- //二维一维
- private static void twoDegreeToOneDegree(int[][] twoDegree,int row,int col){
- int[] oneDegree = new int[row*col];
- int k = 0;
- for(int i=0;i<row;i++){
- for(int j=0;j<col;j++){
- oneDegree[k] = twoDegree[i][j];
- k++;
- }
- }
- }
- //左转90度
- private static void twoDegreeConvertLeft90(int[] oneDegree,int row,int col){
- int k = 0;
- int[][] convert90Degree = new int[row][col];
- for(int i=0;i<col;i++){
- for(int j=row-1;j>=0;j--){
- convert90Degree[j][i] = oneDegree[k];
- k++;
- }
- }
- display(convert90Degree, row, col);
- }
- //右转90度
- private static void twoDegreeConvertRight90(int[] oneDegree,int row,int col){
- int k = 0;
- int[][] convert90Degree = new int[row][col];
- for(int i=col-1;i>=0;i--){
- for(int j=row-1;j>=0;j--){
- convert90Degree[j][i] = oneDegree[k];
- k++;
- }
- }
- display(convert90Degree, row, col);
- }
- //打印
- private static void display(int[][] arr,int row,int col){
- for(int i=0;i<row;i++){
- for(int j=0;j<col;j++){
- System.out.print(arr[i][j]+" ");
- }
- System.out.println();
- }
- System.out.println();
- }
- }
一维数组转二维数组,旋转
最新推荐文章于 2022-03-09 18:46:39 发布