Inicializujte ArrayList v Javě
ArrayList je součástí sbírkový rámec a je přítomen v balíčku java.util . Poskytuje nám dynamická pole v Javě. Může to být pomalejší než standardní pole, ale může být užitečné v programech, kde je potřeba spousta manipulace s polem.
- ArrayList zdědí třídu AbstractList a implementuje rozhraní List.
- ArrayList je inicializován podle velikosti, ale velikost se může zvětšit, pokud kolekce naroste, nebo se zmenšit, pokud jsou z kolekce odstraněny objekty.
- Java ArrayList nám umožňuje náhodný přístup k seznamu.
- ArrayList nelze použít pro primitivní typy, jako je int, char atd. Pro takové případy potřebujeme obalovou třídu (podrobnosti viz toto).
- ArrayList v Javě lze považovat za podobný vektor v C++ .
Níže jsou uvedeny různé metody inicializace ArrayList v Javě:
Inicializace pomocí add()
- Syntax:
ArrayList str = new ArrayList(); str.add('Geeks'); str.add('for'); str.add('Geeks'); - Příklady:
Jáva
// Java code to illustrate initialization> // of ArrayList using add() method> import> java.util.*;> public> class> GFG {> > public> static> void> main(String args[])> > {> > // create a ArrayList String type> > ArrayList gfg => new> ArrayList();> > // Initialize an ArrayList with add()> > gfg.add('Geeks');> > gfg.add('> for> ');> > gfg.add('Geeks');> > // print ArrayList> > System.out.println('ArrayList : ' + gfg);> > }> }> |
Výstup:
ArrayList : [Geeks, for, Geeks]
- Příklady: Použití zkrácené verze této metody
Jáva
// Java code to illustrate initialization> // of ArrayList using add() method> import> java.util.*;> public> class> GFG {> > public> static> void> main(String args[])> > {> > // create a ArrayList String type> > // and Initialize an ArrayList with add()> > ArrayList gfg => new> ArrayList() {> > {> > add('Geeks');> > add('> for> ');> > add('Geeks');> > }> > };> > // print ArrayList> > System.out.println('ArrayList : ' + gfg);> > }> }> |
Výstup:
ArrayList : [Geeks, for, Geeks]
Inicializace pomocí asList()
- Syntax:
ArrayList obj = new ArrayList( Arrays.asList(Obj A, Obj B, Obj C, ....so on));
- Příklady:
Jáva
// Java code to illustrate initialization> // of ArrayList using asList method> import> java.util.*;> public> class> GFG {> > public> static> void> main(String args[])> > {> > // create a ArrayList String type> > // and Initialize an ArrayList with asList()> > ArrayList gfg => new> ArrayList(> > Arrays.asList('Geeks',> > '> for> ',> > 'Geeks'));> > // print ArrayList> > System.out.println('ArrayList : ' + gfg);> > }> }> |
Výstup:
ArrayList : [Geeks, for, Geeks]
Inicializace pomocí metody List.of().
- Syntax:
List obj = new ArrayList( List.of(Obj A, Obj B, Obj C, ....so on));
- Příklady:
Jáva
// Java code to illustrate initialization> // of ArrayList using List.of() method> import> java.util.*;> public> class> GFG {> > public> static> void> main(String args[])> > {> > // create a ArrayList String type> > // and Initialize an ArrayList with List.of()> > List gfg => new> ArrayList(> > List.of('Geeks',> > '> for> ',> > 'Geeks'));> > // print ArrayList> > System.out.println('ArrayList : ' + gfg);> > }> }> |
Výstup:
ArrayList : [Geeks, for, Geeks]
Inicializace pomocí jiné kolekce
- Syntax:
List gfg = new ArrayList(collection);
- Příklady:
Jáva
// Java code to illustrate initialization> // of ArrayList using another collection> import> java.util.*;> public> class> GFG {> > public> static> void> main(String args[])> > {> > // create another collection> > List arr => new> ArrayList();> > arr.add(> 1> );> > arr.add(> 2> );> > arr.add(> 3> );> > arr.add(> 4> );> > arr.add(> 5> );> > // create a ArrayList Integer type> > // and Initialize an ArrayList with arr> > List gfg => new> ArrayList(arr);> > // print ArrayList> > System.out.println('ArrayList : ' + gfg);> > }> }> |
Výstup:
ArrayList : [1, 2, 3, 4, 5]
Inicializace pomocí metod stream() a collect().
1. Syntax:
ArrayList listName = Stream.of(element1, element2, ..., elementN).collect(Collectors.toCollection(ArrayList::new));
1. Příklady:
Jáva
import> java.util.ArrayList;> import> java.util.stream.Collectors;> import> java.util.stream.Stream;> public> class> GFG {> > public> static> void> main(String args[])> > {> > // create a stream of elements using Stream.of()> > // method collect the stream elements into an> > // ArrayList using the collect() method and> > // Collectors.toCollection() method> > ArrayList list> > = Stream.of(> 'Geeks'> ,> 'For'> ,> 'Geeks'> )> > .collect(Collectors.toCollection(> > ArrayList::> new> ));> > System.out.println(list);> // print the ArrayList> > }> }> |
Výstup
[Geeks, For, Geeks]