简体   繁体   中英

How to join two ArrayLists with the same index in Java

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.

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM