As other answers and comments have advised, you can't change the size of array, you'll have to create a new one.
However, it's best to use a Collection, which will automatically resize itself.
If you still want to do it using a new array, here is how you can do it:-
I have taken an array of size 3x3, you can use the code for 100x100
{"row0 col0", "", "row0 col2"},
{"row1 col0", "", ""},
{"", "", ""}
In this example, since the max number of columns used by any row is 2, we can have the new array with just 2 columns instead of 3.
Similarly, last row is virtually empty with no non-empty strings, we can have the new array with just 2 rows instead of 3.
public static void main(String[] args) {
String[][] array1 = new String[][]{
{"row0 col0", "", "row0 col2"},
{"row1 col0", "", ""},
{"", "", ""}
};
long maxNoOfColumnsInAnyRow = Arrays.stream(array1)
.map(strings -> Arrays.stream(strings)
.filter(s -> s != null && !s.isEmpty())
.count())
.max(Long::compareTo)
.orElse((long) array1[0].length);
long noOfNonEmptyRows = Arrays.stream(array1)
.filter(strings -> Arrays.stream(strings)
.anyMatch(s -> s != null && !s.isEmpty()))
.count();
String[][] array2 = new String[(int) noOfNonEmptyRows][(int) maxNoOfColumnsInAnyRow];
int i = 0, j;
for (String[] strings : array1) {
j = 0;
for (String str : strings) {
if (str != null && !str.isEmpty()) {
array2[i][j] = str;
j++;
}
}
if (j != 0) i++;
}
System.out.println(Arrays.deepToString(array2));
}
Output is [[row0 col0, row0 col2], [row1 col0, null]], which is a 2x2 array.