Doubt with ArrayLists in Java

0

my question is this:

Can you dump the content that is in an ArrayList so far into an ArrayList? and then the first one to restart it to put another series of data and dump it in another position of the second one?

I explain the question.

I'm doing a program that stores a series of words depending on the language that is indicated and must be ArrayList ArrayList, so, I read an external file 50 words, of which the first 25 are the language 1 and the 25 seconds of language 2; then, I would like to be able to read the first 25 and dump them to the first position of the ArrayList ArrayList and then read the next 25 and dump them to the 2nd position.

The problem I have is that when I do the first dump, everything is fine, but when doing the second dump, as the arraylist that stores the words already has 50 positions, the one that stores the arraylist, already has two positions of 50 words each.

I hope your help. Thank you very much in advance.

    
asked by Alfred 16.12.2017 в 13:21
source

1 answer

0
import java.util.ArrayList;

public class Arraylists {

    public static void main(String[] args) {

        //Array nommal y array de arrays normales
        ArrayList<String> idiomas = new ArrayList<String>();
        ArrayList<ArrayList<String>> arrayIdiomas = new ArrayList<ArrayList<String>>();

        //Puedes cambiar el valor de 50 por uno mas grande
        for(int i = 0; i < 50; i++){

            if(i < 25) idiomas.add("palabrasEnEspañol");
            if(i >= 25) idiomas.add("palabrasEnIngles");
        }

        //Imprime el array normal
        for(String palabra: idiomas){
            System.out.println(palabra);
        }

        //Array auxiliar
        ArrayList<String> aux = new ArrayList<String>();

        for(int j = 0; j < idiomas.size(); j++){

            if(j == 0 || (j % 25 != 0)){    
                aux.add(idiomas.get(j));
                if(j == idiomas.size()-1){
                    arrayIdiomas.add(aux);
                }
            } else{

                arrayIdiomas.add(aux);
                aux = new ArrayList<String>();
                aux.add(idiomas.get(j));
            }
        }

        //Imprime el array de arrays
        for(ArrayList<String> array: arrayIdiomas){
            System.out.println(array);
        }   
    }
}
    
answered by 16.12.2017 / 15:28
source