gpt4 book ai didi

java - 删除拆分数组上带有 0 的列

转载 作者:行者123 更新时间:2023-11-29 03:22:23 26 4
gpt4 key购买 nike

在一个函数中,我有两个二维数组(超过 50x50)作为输入,它们都具有相同的 Nr。列但不同的 Nr。行。基本上我需要删除包含 0 到 2 范围内数字的公共(public)列

例子:

A: 3 0 0 0 0 1 0
5 0 0 6 0 0 2
2 0 0 7 1 0 0

B: 2 3 0 1 0 0 1
4 9 0 2 0 0 0

在这种情况下,我将不得不删除第 3、6 和 7 列(到 A 和 B),因为第 3 列只有 0,第 6 列只有 0 和 1,而最后一列都是 Nr。从 0 到 2。

我可以使用像 LinearAlgebra.deleteColumns(A , 2 , 5,6);LinearAlgebra.deleteColumns(B, 2, 5,6); 这样的函数但由于我的输入数组很大,我必须对两个数组进行列解析。

有什么想法可以解决这个问题吗?这是使用 3 个 for 循环(java)的原始想法

for(int col=0; col < A[0].length; col++){   // step through each column
int cnt = 0;

for(int row = 0; row < B.length ; row ++){
if(!(B[col][row].equals(0 | 1 | 2))) {
cnt++;
}
}

if(cnt == 0) {
// store number of column -> use later on LinearAlgebra.deleteColumns
}

for(int row = 0; row < A.length ; row++){
if(!(A[col][row].equals(0 | 1 | 2)))
cnt++;
}
if(cnt == 0){
// check aswell all values of B otherwise iterate next column
}
}

最佳答案

boolean result[] = new boolean[<num of columns>];
for (int i = 0; i < result.length; ++i)
result[i] = true;

for (int row = 0; row < arrayA.length; ++row) {
for (int col = 0; col < arrayA[row].length; ++col)
result[col] &= arrayA[row][col] == 0;
}

for (int row = 0; row < arrayB.length; ++row) {
for (int col = 0; col < arrayB[row].length; ++col)
result[col] &= arrayB[row][col] == 0;
}

for (int i = 0; i < 6; ++i)
System.out.println(result[i]);

现在 result 数组中的每个单元格(列)将指示哪个列包含两个数组的零。

*注意:*如您所说,这假设两个数组具有相同的列数

因评论而编辑:

如果要删除值在某个包含范围内的列,请使用此条件:

 result[col] &= arrayA[row][col] >= minRange && arrayA[row][col] <= maxRange;

对于独占范围,只需删除 = 符号

对第二个数组做同样的事情

关于java - 删除拆分数组上带有 0 的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22837086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com