Vytvorenie zoznamu polí z poľa

Mám pole, ktoré je inicializované takto:

Element[] array = {new Element(1), new Element(2), new Element(3)};

Chcel by som toto pole previesť na objekt triedy ArrayList.

ArrayList<Element> arraylist = ???;
Riešenie
new ArrayList(Arrays.asList(array))
Komentáre (20)
new ArrayList(Arrays.asList(myArray));

Uistite sa, že myArray je rovnaký typ ako T. Ak sa napríklad pokúsite vytvoriť List z poľa int, dostanete chybu kompilátora.

Komentáre (0)

Pravdepodobne potrebujete len Zoznam, nie ArrayList. V takom prípade stačí urobiť:

List arraylist = Arrays.asList(array);
Komentáre (7)