简体   繁体   English

我正在尝试使用 function 修改此二维数组以将偶数行上的偶数加倍,并将奇数行上的奇数加倍

[英]I'm trying to use a function to modify this 2d array to double the even numbers on the even rows and double the odd numbers on the odd rows

I'm trying to use a function to modify this 2d array to double the even numbers on the even rows and double the odd numbers on the odd rows.我正在尝试使用 function 修改此二维数组,使偶数行的偶数加倍,奇数行的奇数加倍。 For instance if my 2d array is例如,如果我的二维数组是

 {1,3,6,2}
   ,{7,5,6,1}
   ,{3,3,1,5}
   ,{9,0,5,3}

then it should come out like this那么它应该像这样出来

 {1,2,12,4}
   ,{14,10,6,2}
   ,{3,3,1,5}
   ,{18,0,10,6}

right now my code looks like this现在我的代码看起来像这样

 public static void main(String[] args) {

        int[][] a = {{1,3,6,2}
                    ,{7,5,6,1}
                    ,{3,3,1,5}
                    ,{9,0,5,3}};

        System.out.println(Arrays.deepToString(multiplyMatrix(a)));
    }
    public static int[][] multiplyMatrix(int[][] input){

        for (int row = 0; row < input.length; row++) {
            if (row%2==0){
                for (int col = 0; col < input[row].length; col++) {
                    if (col%2==0){

                    }
                }
            } else {
                for (int col = 0; col < input[row].length; col++) {
                    if (col%2!=0){

                    }
                }
            }

        }

        return null;

    }

as it is I'm trying to use nested for loops and if loops to check the row to see if its even or not then check if the column is even or not but i dont know how to set the rest of it up.因为它是我正在尝试使用嵌套的 for 循环和 if 循环来检查该行以查看它是否偶数然后检查该列是否偶数但我不知道如何设置它的 rest。

A 2d array is an array of arrays.二维数组是 arrays 的数组。

int[] [] array =... ;
  1. Get the row and column counts获取行数和列数

    int yCount = array.length; int yCount = array.length; int xCount = array[0].length; int xCount = array[0].length;

  2. Copy the array because changing values of something, you are iterating, can cause problems.复制数组,因为改变某些东西的值,你正在迭代,可能会导致问题。

    int[] [] copied Array =... ; int[] [] 复制数组 =... ;

  3. Loop over everything.遍历所有内容。

    If a number%2 equals 0, then the number is even.如果数字 %2 等于 0,则该数字为偶数。

You can use copiedArray[i][y] for setting the numbers.您可以使用 copiedArray[i][y] 来设置数字。

I and Y are the countervariables of your loops. I 和 Y 是循环的反变量。

Example: This checks the array numbers, so it begins with 0. Please note: The "copied array" is the argument array of the function (call by value).示例:这检查数组编号,因此它从 0 开始。请注意:“复制的数组”是 function(按值调用)的参数数组。

public static void main(String[] args) {
    int[][] array2d = { { 1, 3, 6, 2 }, { 7, 5, 6, 1 }, { 3, 3, 1, 5 }, { 9, 0, 5, 3 } };

    System.out.println(Arrays.deepToString(array2d));
    System.out.println(Arrays.deepToString(multiplyMatrix(array2d)));
}

public static int[][] multiplyMatrix(int[][] array2d) {
    int rowsCount = array2d.length; // == 4
    int columnsCount = array2d[0].length; // == 4
    
    for (int i = 0; i < columnsCount; i++) {
        for (int j = 0; j < rowsCount; j++) {
            if (i % 2 == 0 && array2d[i][j] % 2 == 0) {
                array2d[i][j] = array2d[i][j] * 2;
            }
            if (i % 2 != 0 && array2d[i][j] % 2 != 0) {
                array2d[i][j] = array2d[i][j] * 2;
            }
        }
    }
    return array2d;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM