简体   繁体   中英

How would you print the columns in a 2D array that don't contain a certain value?

I'm trying to exclude columns that contain the value -1 from printing its elements within that column. Tried using enhanced for loop to go through the array and using.equals to identify if the column contain the value, but something is going wrong and it'll print the entire array anyways.`

    public static void main(String[] args) {

    int[][] calificaciones = {{1, 9, 1, 3},
                              {7, -1, -1, -1},
                              {9, 4, 1, 1},
                              {1, 10, 10, 2}};
    int none=-1;

    for( int i[]: calificaciones){
        if(i.equals(none)){
            continue;
        }
        System.out.println(Arrays.toString(i));
    }
}

-1 will never be equal to an int[] . In fact two equivalent int[] (s) will not be equal. Since arrays don't override Object.equals(Object) it's a strict reference test. For example,

int[] a = { 1, 2, 3 }, b = { 1, 2, 3 };
System.out.printf("%s %s = %s%n", a, b, a.equals(b));
System.out.printf("%s %s = %s%n", Arrays.toString(a), 
        Arrays.toString(b), Arrays.equals(a, b));

Outputs

[I@7344699f [I@6b95977 = false
[1, 2, 3] [1, 2, 3] = true

Further, arrays are not usually iterated by column. Instead of trying to solve everything in a single loop, I would decompose the problem (break it down into simpler steps). For example, first write a function to determine if a specific element is present in a given column. Something like,

private static boolean columnContains(int[][] arr, int elem, int col) {
    for (int i = 0; i < arr.length; i++) {
        if (col < arr[i].length && arr[i][col] == elem) {
            return true;
        }
    }
    return false;
}

Next write a method to print a given column. Something like,

private static void printColumn(int[][] arr, int col) {
    System.out.printf("Column[%d] [", col);
    for (int i = 0; i < arr.length; i++) {
        if (col < arr[i].length) {
            if (i != 0) {
                System.out.print(", ");
            }
            System.out.print(arr[i][col]);
        }
    }
    System.out.println("]");
}

Now you can invoke the previous methods with another loop. This will search each column for -1 ; only if it isn't present will the column be printed.

public static void main(String args[]) {
    int[][] calificaciones = { //
            { 1, 9, 1, 3 }, //
            { 7, -1, -1, -1 }, //
            { 9, 4, 1, 1 }, //
            { 1, 10, 10, 2 } //
    };
    int i = 0;
    for (int j = 0; j < calificaciones[i].length; j++) {
        if (!columnContains(calificaciones, -1, j)) {
            printColumn(calificaciones, j);
        }
    }
}

Outputs

Column[0] [1, 7, 9, 1]

I recommend reading The Developer Insight Series, Part 1: Write Dumb Code -- Advice From Four Leading Java Developers .

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