Since you want datosnew
to store the data of the non-blank positions ("") of the array datos
, the size of the array datosnew
must be 3, since that is the number of positions with data in the array datos
. If you create the array datosnew
with the same size as the array datos
, at the end you will have the same number of positions, but instead of being empty ("") these would have the value of null
, because if you create an array of type String and you do not fill its positions, the default value of these will be null.
Then, as you are doing, you go through the array datos
with a cycle and with a conditional you evaluate if the positions are not empty (""). If the position is not empty, you store the data for that position in the array datosnew
; out of conditional you create a counter that increases its value each time the condition of the if is fulfilled, to store the data of the array datos
in different positions of the array datosnew
.
String datos[] = {"hola", "", "como", "", "", "estas", ""};
String[] datosnew = new String[3];
int j = 0;
for (int i = 0; i < datos.length; i++) {
if (!datos[i].equals("")) {
datosnew[j] = datos[i];
j++;
}
}
!
is a boolean operator that inverts a boolean operation, if a boolean operation is true it becomes false and vice versa.