Home  >  Article  >  Java  >  How to convert 2D array to 1D array in Java

How to convert 2D array to 1D array in Java

PHPz
PHPzforward
2023-04-26 12:40:082218browse

Convert a two-dimensional array into a one-dimensional array

1. To be lazy, I wrote a function that randomly generates a two-dimensional array

 /*
    * 自动创建随机为100以内的二维数组: int nums[x][y]
    * */
    public static int[][] CreatedDemArray(int x,int y){
        int nums[][]=new int[x][y];
        for(int i=0;i<nums.length;i++){
            for(int j=0;j<nums[i].length;j++){
                nums[i][j]=(int)(Math.random()*100);
            }
        }
        return nums;
    }

2. Define a 5x8 two-dimensional function

 public static void main(String[] args) {
        createdarray ca=new createdarray();
        int nums[][]=CreatedDemArray(5,8);
    }

3. Define an int array with a length of 40, and use System.arraycopy to fill the two-dimensional array into the one-dimensional array temp

  public static void main(String[] args) {
        createdarray ca=new createdarray();
        int nums[][]=CreatedDemArray(5,8);
       //定义整数数组
        int[] temp=new int[40];
       //通过 arraycopy方法来将二维数组填充到一维数组temp中
        for(int i=0;i<nums.length;i++){
            System.arraycopy(nums[i],0,temp,i*8,8);
        }
 
    }

4. If it is filled to one dimension After the array, I want to do something. After sorting the one-dimensional array, backfill it into the two-dimensional array

   public static void main(String[] args) {
        createdarray ca=new createdarray();
        int nums[][]=CreatedDemArray(5,8);
        int[] temp=new int[40];
        for(int i=0;i<nums.length;i++){
            System.arraycopy(nums[i],0,temp,i*8,8);
        }
       // 对一维数组进行排序
        Arrays.sort(temp);
 
      // 将排序后的数组进行回填
        int index=0;
        while (index<temp.length){
            for(int i=0;i<nums.length;i++){
                for(int j=0;j<nums[i].length;j++){
                    nums[i][j]=temp[index++];
                }
            }
        }
        System.out.println("排序后的数组如下");
       // 这里因为懒所以我自定义了一个函数 打印二维数组
        ShowDemArray(nums);
    }

5. Add to the printing of the two-dimensional array used above

    /*
    * 输出二维数组 
    * 这里我不知道怎么写我还去问了我前任:) 因为我实在不知怎么传这个参
    * 如果要做成通用的比如说既能打印整数类型又能打印字符串类型的二维数组需要用的泛型... 
    * 毕业2年我已经忘了泛型怎么用,下次再补补
 */
public static void ShowDemArray(int[][] nums){
        for (int i=0;i<nums.length;i++){
            for (int j=0;j<nums[i].length;j++){
                System.out.print(nums[i][j]+"\t");
                if((j+1)%nums[i].length==0){
                    System.out.println();
                }
 
            }
        }
    }

Today is the same Think of it as a day of testing

Convert a one-dimensional array into a two-dimensional array

Because when a matrix is ​​often used for calculations, the one-dimensional array is first converted into a two-dimensional array. Therefore, I record it here and hope it will be helpful to others.

package deal;
/*
 * author:合肥工业大学 管院学院 钱洋 
 *1563178220@qq.com
*/
public class ArryTest {

    public static void main(String[] args) {
        //创建一个一维数组 0,1,2,3...,10
        double [] c= new double[10];
        for (int i = 0; i < c.length; i++) {
            c[i]=i;
        }
        double[][] testArr=TwoArry(c);
        for (int i = 0; i < testArr.length; i++) {
            for (int j = 0; j < testArr[i].length; j++) {
                System.out.println(testArr[i][j]);
            }
        }

    }
    //一维数组转化为二维数组
    public static double[][] TwoArry(double[] onedouble){
        double[][] arr=new double[1][onedouble.length];
        for (int i = 0; i < onedouble.length; i++) {
            arr[0][i]=onedouble[i];
        }
        return arr;
    }
}

Program running results.

How to convert 2D array to 1D array in Java

The above is the detailed content of How to convert 2D array to 1D array in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete