0

I want to calculate the length of rows and columns in a 2d array. I know how to do the row part however I can't figure out how to find the length of a column in this array.

public class array2D {
    public static void main(String args[]) {
        int maxNumberOfColumns = 8;
        Integer[][] j = {
                {2, 4, 8, 5, 1, 3, 7, 2},
                {6, 7, 4, 8, 2},
                {2, 3, 5, 9, 7, 1}};

        for (int row = 0; row < j.length; row++) {
            for (int col = 0; col < j[row].length; col++) {
                System.out.print(j[row][col] + "  ");
            }
            System.out.println();
        }

        System.out.println();
        System.out.println("------------");
        System.out.println();

        for (int row = 0; row < j.length; row++) {
            System.out.println("row "+row+" has "+j[row].length+" elements");
        }
        System.out.println();

        for (int col = 0; col < maxNumberOfColumns; col++) {
            //code I don't know
        }
    }
}

3 Answers3

0

You can change your last for loop to below to print the count of each column. I use an internal loop here to calculate the number of elements

for (int col = 0; col < maxNumberOfColumns; col++) {
    int count = 0;
    for (int i = 0; i < j.length; i++) {
        if (j[i].length > col) {
            count++;
        }
    }
    System.out.println("Column " + (col + 1) + " has " + count + " elements");
}

An alternative solution is to calculate the minimum number of columns in the first loop using

minNumberOfColumns = length < minNumberOfColumns ? length : minNumberOfColumns;

and then change the last loop to

System.out.println("Column 1 to " + minNumberOfColumns + " has " + j.length + " elements");
for (int col = minNumberOfColumns; col < maxNumberOfColumns; col++) {
    int count = 0;
    for (int i = 0; i < j.length; i++) {
        if (j[i].length > col) {
            count++;
        }
    }
    System.out.println("Column " + (col + 1) + " has " + count + " elements");
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
0

You don't need the maxNumberOfColumns variable. Instead, you can count the elements as long as they are present, or 0 otherwise:

Integer[][] arr = {
        {2, 4, 8, 5, 1, 3, 7, 2},
        {6, 7, 4, 8, 2},
        {2, 3, 5, 9, 7, 1}};

// count of elements in the columns
int[] count = Arrays.stream(arr)
        .filter(Objects::nonNull)
        // for each row take an array
        // of the same length, filled with '1'
        .map(row -> IntStream
                .range(0, row.length)
                .map(i -> 1)
                .toArray())
        // summing elements in rows, if any, or '0' otherwise
        .reduce((row1, row2) -> IntStream
                .range(0, Math.max(row1.length, row2.length))
                .map(i -> (i < row1.length ? row1[i] : 0)
                        + (i < row2.length ? row2[i] : 0))
                .toArray())
        .orElse(null);

System.out.println(Arrays.toString(count));
// [3, 3, 3, 3, 3, 2, 1, 1]

See also: Filling a jagged 2d array first by columns

-1

This code will solve your problem :

int maxNumberOfColumns = 8;
Integer[][] j = {
        {2, 4, 8, 5, 1, 3, 7, 2},
        {6, 7, 4, 8, 2},
        {2, 3, 5, 9, 7, 1}};

for (int row = 0; row < j.length; row++) {
    for (int col = 0; col < j[row].length; col++) {
        System.out.print(j[row][col] + "  ");
    }
    System.out.println();
}

System.out.println();
System.out.println("------------");
System.out.println();
int max = 0;
for (int row = 0; row < j.length; row++) {
    System.out.println("row " + row + " has " + j[row].length + " elements");
    if (j[row].length > max) {
        max = j[row].length;
    }
}
for (int k = 0; k < max; k++) {
    int count = 0;
    for (int row = 0; row < j.length; row++) {
        try {
            if (j[row][k] != null) {
                count = count + 1;
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            continue;
        }
    }
    System.out.println("Column " + k + " has " + count + " elements");
}
System.out.println();
Community
  • 1
  • 1
Sarangan
  • 868
  • 1
  • 8
  • 24