简体   繁体   中英

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. 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.

A 2d array is an array of arrays.

int[] [] array =... ;
  1. Get the row and column counts

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

  2. Copy the array because changing values of something, you are iterating, can cause problems.

    int[] [] copied Array =... ;

  3. Loop over everything.

    If a number%2 equals 0, then the number is even.

You can use copiedArray[i][y] for setting the numbers.

I and Y are the countervariables of your loops.

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).

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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