Jump to content

Adicionar ArrayList noutro ArrayList


ricardoaux

Recommended Posts

Boas

Tenho duas arraylists:

ArrayList<ArrayList<Integer>> array1 = new ArrayList<ArrayList<Integer>>();

ArrayList<Integer> array2 = new ArrayList<Integer>();

O que eu quero é adicionar elementos do array2 no array1.. faço:

array1.add(array2);

Mas está a adicionar um array vazio, em vez do conteúdo do array2 :S

Como faço para conseguir adicionar o conteúdo do array2 ao array1???

Desde já agradeço a ajuda.

Link to comment
Share on other sites

Adicionaste alguma coisa ao array2? O que estás a fazer funciona perfeitamente:

        ArrayList<ArrayList<Integer>> array1 = new ArrayList<ArrayList<Integer>>();
       ArrayList<Integer> array2 = new ArrayList<Integer>();
       ArrayList<Integer> array3 = new ArrayList<Integer>();

       array2.add(1);
       array2.add(3);

       array3.add(4);
       array3.add(6);

       array1.add(array2);
       array1.add(array3);

       for (ArrayList<Integer> a : array1) {
           System.out.println("A imprimir array:");
           for (int b : a) {
               System.out.println(b);
           }
       }

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.