Java 如何删除多维字符串数组中的空元素? String[]]array={{“abcd”、“”}、{“asdf”、“”}、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、;
我想从数组中删除Java 如何删除多维字符串数组中的空元素? String[]]array={{“abcd”、“”}、{“asdf”、“”}、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、;,java,arrays,string,multidimensional-array,filtering,Java,Arrays,String,Multidimensional Array,Filtering,我想从数组中删除{“”“”}元素 如何在Java中执行此操作?删除? 不能更改现有阵列的大小。 如果要创建仅包含这些元素的新数组,请计算每个数组的长度,根据这些长度创建新数组,然后将元素添加到新数组中 String[][] array= {{"abcd",""},{"asdf",""},{"",""},{"",""},{"",""},{"",""}}; //Assuming you want a 1-D array int valuesPresent = 0; for (int i = 0; i
{“”“”}
元素
如何在Java中执行此操作?删除?
不能更改现有阵列的大小。
如果要创建仅包含这些元素的新数组,请计算每个数组的长度,根据这些长度创建新数组,然后将元素添加到新数组中
String[][] array= {{"abcd",""},{"asdf",""},{"",""},{"",""},{"",""},{"",""}};
//Assuming you want a 1-D array
int valuesPresent = 0;
for (int i = 0; i < arrray.length; i++) {
for (int j = 0; i < arrray[i].length; i++) {
if (array[i][j] != "") {
valuesPresent++;
}
}
}
//Now you know how many values are there, so initialize a new array of that size
String[] answer = new String[valuesPresent];
//Now add all the values to it
int index = 0;
for (int i = 0; i < arrray.length; i++) {
for (int j = 0; i < arrray[i].length; i++) {
if (array[i][j] != "") {
answer[index] = array[i][j];
index++;
}
}
}
String[][] copyArray = Arrays.stream(array)
.filter(arr -> arrayPredicate.test(Arrays.stream(arr)))
.toArray(String[][]::new);
array = copyArray; // reassign to array
String[]]array={{“abcd”、“”}、{“asdf”、“”}、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、;
//假设你想要一个一维数组
int valuesPresent=0;
for(int i=0;i
要获得易于理解的二维阵列,请执行以下操作:
//Just reordered input so we can understand better
String[][] array= {{"abcd","zxcs"}, //Row 0, col 0 = abcd and col 1 = zxcs
{"asdf",""}, //Row 1, col 0 = asdf and col 1 = ""
{"",""}}; //Row 2, col 0 = "" and col 2 = ""
//Counts how many columns have values(are not equal to "") in each row
int rowsWithValues = 0; //Counts how many rows have at least 1 value. Here, 2
for (int row = 0; row < arrray.length; row++) {
for (int col = 0; col < arrray[row].length; col++) {
if (array[row][col] != "") {
rowsWithValues++; //Since there's a col with value for this row
break; //If any one value has been found, no need to check other cols
}
}
}
//Now we know how many rows we need in the result array: 2 (row 2 has no values)
String[][] result = new String[2][];
//We need to add the 2 rows with values now
int arrayIndex = 0; //Points to next empty index in result[][]
for (int row = 0; row < arrray.length; row++) {
int colsWithValues = 0; //Cols with values for each row
for (int col = 0; col < arrray[i].length; col++) {
if (array[row][col] != "") {
colsWithValues ++; //Col with value found for this row
}
}
//Eg. For row 0, colsWithValues will be 2, since 2 cols have values(abcd, zxcs)
//We need to add these 2 cols as a single array to result
String[] currentRow = new String[colsWithValues]; //Will be 2 here for row 0
int rowIndex = 0; //Points to next empty column in currentRow[]
for (int col = 0; col < array[row].length; col++) {
if (array[row][col] != "") {
currentRow[rowIndex] = array[row][col];
}
}
//After this, for row 0, currentRow will be {abcd, zxcs}
//Just add it to our result
result[arrayIndex] = currentRol;
//After 1st iteration, result will be {{"abcd", "zxcs"}, {}}
//During 2nd iteration, arrayIndex == 1, currentRow == {"asdf"}
//On adding it to result[1], result will be {{"abcd", "zxcs"}, {"asdf"}}
//只需重新排序输入,以便我们更好地理解
字符串[][]数组={{“abcd”,“zxcs”},//行0,列0=abcd,列1=zxcs
{“asdf”,“”},//第1行,列0=asdf和列1=“”
{"",""}}; //第2行,列0=“”和列2=“”
//统计每行中有多少列具有值(不等于“”)
int rowsWithValues=0//统计至少有1个值的行数。这里,2
对于(int row=0;row
类似的东西应该可以工作我在这里假设,嵌套数组可以是任意大小的,而不仅仅是两个元素 创建一个谓词,该谓词接受
流
,并检查任何元素是否为null和非空
String[][] array= {{"abcd",""},{"asdf",""},{"",""},{"",""},{"",""},{"",""}};
Predicate<Stream<String>> arrayPredicate = element ->
element.anyMatch(ele ->Objects.nonNull(ele) && !ele.isEmpty());
首先,不要使用
=
或!=
比较两个字符串是否相等,即使是字符串数组:
if (array[i][j] != "") {
在上述情况下,应为:
if (!array[i][j].equals("")) {
如果您还没有完全了解Streams,那么这是您可能感兴趣的一种方式:
public static String[][] removeNullStringRows(String[][] array) {
if (array == null || array.length == 0) {
return null;
}
int validCount = 0; // Row Index Counter for the new 2D Array
/* Find out how may rows within the 2D array are valid
(where the do not consist of Null Strings {"", ""}).
This let's you know how many rows you need to initialize
your new 2D Array to. */
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (!array[i][j].equals("")) {
validCount++;
break;
}
}
}
/* Declare and initialize your new 2D Array. This is
assuming the column count is the same in all rows. */
String[][] array2 = new String[validCount][array[0].length];
validCount = 0; // Used as an index increment counter for the new 2D Array
// Iterate through the supplied 2D Array and weed out
// the bad (invalid) rows.
for (int i = 0; i < array.length; i++) { // Iterate Rows...
for (int j = 0; j < array[i].length; j++) { // Iterate Columns
/* Does this row contain anything other than a Null String ("")?
If it does then accept the entire Row into the new 2D Array. */
if (!array[i][j].equals("")) {
// Retrieve all the columns for this row
for (int k = 0; k < array[i].length; k++) {
array2[validCount][k] = array[i][k];
}
// The above small 'for' loop can be replaced with:
// System.arraycopy(array[i], 0, array2[validCount], 0, array[i].length);
validCount++; // Increment our Row Index Counter for the new 2D Array
break; // Get out of this column iterator. We already know it's good.
}
}
}
return array2; // Return the new 2D Array.
}
您可以从该2d数组中筛选出所有
null
和空元素,如下所示:
String[]]array={{“abcd”、“”}、{“asdf”、“”}、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、{”、“asdf”、;
String[]nonEmptyArray=Arrays.stream(数组)
.map(行->数组.stream(行)
//过滤掉“null”元素和空字符串
.filter(e->e!=null&&e.length()>0)
//非空字符串数组或空
//数组,如果没有这样的字符串
.toArray(字符串[]::新建))
//过滤掉空数组
.filter(行->行长度>0)
//非空数组的数组
.toArray(字符串[]:[]::新建);
//输出
System.out.println(Arrays.deepToString(nonEmptyArray));//[[abcd],[asdf]]
不,如果你有{{“abcd”,“aa”},{“asdf”,“},{”,“},{”,“},{”,“},{”,“},{”,“},};
你的值将是3,但实际上你有2个子数组;)先生,我可以初始化一个可变长度的二维数组,将多维数组作为output@DevilsHnd是,(数组[i][j]!=“”)
意味着它是空的,对吗?@azro这是用于一维数组的;valuesPresent==3,这将生成一个带有{“abcd”,“aa”,“asdf”}的一维数组,这是1D数组的预期值。@OmkarGhurye您能再解释一点问题是什么吗?它经过测试,在示例中有效
public static String[][] removeNullStringRows(String[][] array) {
if (array == null || array.length == 0) {
return null;
}
int validCount = 0; // Row Index Counter for the new 2D Array
/* Find out how may rows within the 2D array are valid
(where the do not consist of Null Strings {"", ""}).
This let's you know how many rows you need to initialize
your new 2D Array to. */
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (!array[i][j].equals("")) {
validCount++;
break;
}
}
}
/* Declare and initialize your new 2D Array. This is
assuming the column count is the same in all rows. */
String[][] array2 = new String[validCount][array[0].length];
validCount = 0; // Used as an index increment counter for the new 2D Array
// Iterate through the supplied 2D Array and weed out
// the bad (invalid) rows.
for (int i = 0; i < array.length; i++) { // Iterate Rows...
for (int j = 0; j < array[i].length; j++) { // Iterate Columns
/* Does this row contain anything other than a Null String ("")?
If it does then accept the entire Row into the new 2D Array. */
if (!array[i][j].equals("")) {
// Retrieve all the columns for this row
for (int k = 0; k < array[i].length; k++) {
array2[validCount][k] = array[i][k];
}
// The above small 'for' loop can be replaced with:
// System.arraycopy(array[i], 0, array2[validCount], 0, array[i].length);
validCount++; // Increment our Row Index Counter for the new 2D Array
break; // Get out of this column iterator. We already know it's good.
}
}
}
return array2; // Return the new 2D Array.
}
// Your current 2D Array
String[][] array = {
{"abcd",""}, {"asdf",""}, {"",""},
{"",""}, {"",""}, {"",""}
};
// If the supplied 2D Array is null contains no rows
// then get out of here.
if (array == null || array.length == 0) {
return;
}
// Display the original 2D Array (array) in the Console window
System.out.println("The original 2D Array:");
for (int i = 0; i < array.length;i++) {
System.out.println(Arrays.toString(array[i]));
}
// Remove all rows that contain all Null String Columns.
// Make your Array equal what is returned by our method.
array = removeNullStringRows(array);
// Display the new 2D Array (array) in the Console window.
System.out.println();
System.out.println("The New 2D Array:");
for (int i = 0; i < array.length;i++) {
System.out.println(Arrays.toString(array[i]));
}
The original 2D Array:
[abcd, ]
[asdf, ]
[, ]
[, ]
[, ]
[, ]
The New 2D Array:
[abcd, ]
[asdf, ]