Java 过滤出数组
我有一个布尔数组[真,假,假,真,真] 并希望使用它拆分二维阵列。 我试着做的是Java 过滤出数组,java,arrays,Java,Arrays,我有一个布尔数组[真,假,假,真,真] 并希望使用它拆分二维阵列。 我试着做的是 public static void sorting(boolean[] test, String[][] arr) { int counter = 0; //finds how many people passed for(int z = 0; z < arr.length; z++) { if (test[z] != fa
public static void sorting(boolean[] test, String[][] arr)
{
int counter = 0;
//finds how many people passed
for(int z = 0; z < arr.length; z++)
{
if (test[z] != false)
counter++;
}
String[][] passed = new String[counter][2];
//tests for which people had Passed and copies it over
for(int x = 0; x < passed.length; x++)
{
for(int y = 0; y < passed[x].length; y++)
if(arr[x] != false)
passed[x][y] = arr[x][y];
}
example2dArrayPrint(passed);
}
输出将是
Bob A
John C
Tom B
我不明白这为什么不能正确排序。
编辑
这两个数组之间的关系是,如果test[0]==true,则arr[0][0]和arr[0][1]的该部分将放入新传递的数组中,将跳过false。编辑2
将传递从3更改为2,在执行此操作时输入错误。您必须保留两个不同的指针-一个用于传递的
数组中的当前位置,另一个用于输入的arr
。
试试这个:
public static void sorting(boolean[] test, String[][] arr) {
int counter = 0;
//finds how many people passed
for (int z = 0; z < arr.length; z++) {
if (test[z])
counter++;
}
String[][] passed = new String[counter][2];
int i = 0;
//tests for which people had Passed and copies it over
for (int x = 0; x < arr.length; x++) {
if (test[x]) {
for (int y = 0; y < 2; y++) {
passed[i][y] = arr[x][y];
}
i++;
}
}
example2dArrayPrint(passed);
}
publicstaticvoid排序(boolean[]test,String[]arr){
int计数器=0;
//查找通过的人数
对于(int z=0;z 对于(inti=0;i,这里有一种使用streams的不同方法。我将该方法重命名为filtering,因为这是您正在做的,而不是排序
String[][] data = { { "Bob", "A" }, { "Fred", "F" },
{ "Larry", "F" }, { "John", "C" }, { "Tom", "B" } };
boolean[] test = { true, false, false, true, true };
String[][] result = filtering(test, data);
for (String[] a : result) {
System.out.println(Arrays.toString(a));
}
印刷品
[Bob, A]
[John, C]
[Tom, B]
- 使用
test
数组的数组长度,从0到4(含4)对整数数组进行流式处理
- 然后在测试数组中的布尔值上过滤这些索引
- 映射与每个索引关联的数组并返回数组
请描述参数test和参数arredited之间的关系,以显示test和arr@YorkChenQuestion之间的关系仍然不清楚,请简要解释为什么passed有3列,应该是2列,不是吗?修复了@DevParzival、@WJS的错误,我想用F来整理行。查看您提供的输出需要2 cols。
public static void sorting(boolean[] test, String[][] arr){
int counter = 0;
//finds how many people passed
for(int z = 0; z < arr.length; z++){
if (test[z])
counter++;
}
//cols represents number of columns you have in your 2d matrix.
int cols=2;
String[][] passed = new String[counter][2];
counter=0;
//out loop for iterating over arr
for(int i=0;i<arr.length;i++)
if(test[i]){
//for all cols
for(int j=0;j<cols;j++){
passed[counter][j]=arr[i][j];
}
counter++;
}
example2dArrayPrint(passed);
}
String[][] data = { { "Bob", "A" }, { "Fred", "F" },
{ "Larry", "F" }, { "John", "C" }, { "Tom", "B" } };
boolean[] test = { true, false, false, true, true };
String[][] result = filtering(test, data);
for (String[] a : result) {
System.out.println(Arrays.toString(a));
}
[Bob, A]
[John, C]
[Tom, B]
public static String[][] filtering(boolean[] test,
String[][] arr) {
return IntStream.range(0, test.length).filter(i -> test[i])
.mapToObj(i -> arr[i]).toArray(String[][]::new);
}