简体   繁体   English

从二维数组返回包含最大偶数个数的行索引

[英]From Two-Dimensional array return row indexes which contains the biggest number of even numbers

Task:任务:

Complete the method mostEven() below, which will return the row number (index) of the row in the two-dimensional array of integers data which contains the most even numbers.完成下面的mostEven()方法,返回二维整数数组数据中偶数最多的行的行号(索引)。 For example, given the declaration int[][] test = { {10,11,4,0,5},{0,13,11,3,21},{3,8,4,9,18,12},{1,2,3,4,5}};例如,给定声明int[][] test = { {10,11,4,0,5},{0,13,11,3,21},{3,8,4,9,18,12},{1,2,3,4,5}}; the call mostEven(test) should return 2 since the row with index 2 contains the highest number of Even numbers (4 to be specific)调用mostEven(test)应该返回 2,因为索引为 2 的行包含最多数量的偶数(具体为 4)

This is what I have been trying but it is not working it is returning 4 instead of 2这是我一直在尝试的方法,但它不起作用它返回 4 而不是 2

package lab2;

class CCMTSL{

    public static void main(String[] args) {
        int [][] test= {{10,11,4,0,5},{0,13,11,3,21,},{3,8,4,9,18,12},{1,2,3,4,5}};
        
        int saffa=mosteven( test );
        System.out.println(test[saffa][saffa]);
    }

    public static int mosteven(int test [][]) {
        int index1=0;
        
        for( int i=0;i<test.length;i++) {
            for(int j=0;j<test.length;j++)
                if(test[i][i]%2==0 || test[j][j]%2==0 ) {
                    index1=i; 
                    index1=j;
                }
        }
        return index1;
    }
}

You can do it like this:你可以这样做:

import java.util.ArrayList;

class Main {

    public static void main(String[] args) {
        int[][] test = {{10, 11, 4, 0, 5}, {0, 13, 11, 3, 21}, {3, 8, 4, 9, 18, 12}, {1, 2, 3, 4, 5}};

        ArrayList<Integer> evens = mostEven(test);
        System.out.println("Rows with the most number of even numbers:" + evens);
    }

    public static ArrayList<Integer> mostEven(int[][] input) {
        ArrayList<Integer> indexes = new ArrayList<>();

        indexes.add(0);
        int maxEven = countEvenInArray(input[0]);


        for (int i = 1; i < input.length; i++) {
            int evenInRow = countEvenInArray(input[i]);
            if (evenInRow > maxEven) {
                indexes.clear();
                indexes.add(i);
                maxEven = evenInRow;
            } else if (evenInRow == maxEven) {
                indexes.add(i);
            }

        }
        return indexes;
    }

    public static int countEvenInArray(int[] input) {
        int evens = 0;
        for (int j : input) {
            if (j % 2 == 0)
                evens++;
        }
        return evens;
    }
}

I am returning ArrayList of indexes course there can be more indexes with the same number of even numbers.我返回 ArrayList 的索引当然可以有更多的索引具有相同数量的偶数。 I added a function which counts even numbers in a one-dimensional array and then I use it in mostEven function.我添加了一个 function,它计算一维数组中的偶数,然后我在mostEven function 中使用它。

package lab3; package 实验室 3;

public class CCMTSL {公共 class CCMTSL {

public static void main(String[] args) {
    int [][] test= {{10,11,4,0,5},{0,13,11,3,21,},{3,8,4,9,18,12},{1,2,3,4,5}};
    int saffa=mostEven(test);
    System.out.println(saffa);
    
}
public static int mostEven(int [][] data) {
    int mostEvenrow=0;
    int max=0;
    for (int i=0;i<data.length;i++) {
        int index=0;
        for (int j=0;j<data[i].length;j++) {
            if(data[i][j]%2==0) {
                index++;
                if(index>max) {
                    max=index;
                    mostEvenrow=i;
                }
            }
        }
    }
return mostEvenrow;
}
    }

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

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