[英]How do I calculate the column length of a ragged 2d array?
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
}
}
}
You can change your last for loop to below to print the count of each column.您可以将最后一个 for 循环更改为以下以打印每列的计数。 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");
}
You don't need the maxNumberOfColumns
variable.您不需要
maxNumberOfColumns
变量。 Instead, you can count the elements as long as they are present, or 0
otherwise:相反,只要元素存在,您就可以计算元素,否则为
0
:
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另请参阅:首先按列填充锯齿状二维数组
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.