I have the following ArrayLists:
ArrayList <String> restricciones_numeros = new ArrayList();
ArrayList <String> restricciones_signos = new ArrayList();
And they contain:
restricciones_numeros: [, , , , , , , , 230, 230, 230, 5000, 4000, 4000, , 17000, ]
restricciones_signos : [, , , , , , , , -, -, -, , , , , , ]
They have the same size and how you can realize there are empty items, but it dosen't matter.
I need to join each ArrayList with the same index, the result must be:
[, , , , , , , , 230-, 230-, 230-, 5000 , 4000 , 4000 , , 17000, ]
I tried this:
for(int i = 0; i < restricciones_signos.size(); i ){
numeros_signos.add(restricciones_numeros.get(i));
numeros_signos.add(restricciones_signos.get(i));
}
System.out.println("numero y signo de cada restriccion: " numeros_signos);
But this for loop returns me:
[, , , , , , , , , , , , , , , , 230, -, 230, -, 230, -, 5000, , 4000, , 4000, , , , 17000, , , ]
This is only an example, but the program must to be generic, the arraylists can have other numbers, other sizes and other empty items. But always both of them have the same size.
CodePudding user response:
You can transform your algorithm like this :
for(int i = 0; i < restricciones_signos.size(); i ) {
numeros_signos.add(restricciones_numeros.get(i) restricciones_signos.get(i));
}