Liste des tableaux fait partie de cadre de collecte et est présent dans le package java.util. Il nous fournit des tableaux dynamiques en Java. Cependant, il peut être plus lent que les tableaux standard, mais peut être utile dans les programmes où de nombreuses manipulations dans le tableau sont nécessaires.
- ArrayList hérite de la classe AbstractList et implémente l'interface List.
- ArrayList est initialisé par une taille, mais la taille peut augmenter si la collection s'agrandit ou diminuer si des objets sont supprimés de la collection.
- Java ArrayList nous permet d'accéder aléatoirement à la liste.
- ArrayList ne peut pas être utilisé pour les types primitifs, comme int, char, etc. Nous avons besoin d'une classe wrapper pour de tels cas (voir ceci pour plus de détails).
- ArrayList en Java peut être considéré comme similaire à vecteur en C++ .
Vous trouverez ci-dessous les différentes méthodes pour initialiser une ArrayList en Java :
Initialisation avec add()
- Syntaxe:
ArrayList str = new ArrayList(); str.add('Geeks'); str.add('for'); str.add('Geeks');>
- Exemples:
Java
// 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);> > }> }> |
>
>Sortir:
ArrayList : [Geeks, for, Geeks]>
- Exemples : utilisation d'une version abrégée de cette méthode
Java
// 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);> > }> }> |
>
>Sortir:
ArrayList : [Geeks, for, Geeks]>
Initialisation à l'aide de asList()
- Syntaxe:
ArrayList obj = new ArrayList( Arrays.asList(Obj A, Obj B, Obj C, ....so on));>
- Exemples:
Java
// 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);> > }> }> |
>
>Sortir:
ArrayList : [Geeks, for, Geeks]>
Initialisation à l'aide de la méthode List.of()
- Syntaxe:
List obj = new ArrayList( List.of(Obj A, Obj B, Obj C, ....so on));>
- Exemples:
Java
// 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);> > }> }> |
>
>Sortir:
ArrayList : [Geeks, for, Geeks]>
Initialisation à l'aide d'une autre collection
- Syntaxe:
List gfg = new ArrayList(collection);>
- Exemples:
Java
que signifie xdxd
// 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);> > }> }> |
>
>Sortir:
ArrayList : [1, 2, 3, 4, 5]>
Initialisation à l'aide des méthodes stream() et collect()
1. Syntaxe:
ArrayList listName = Stream.of(element1, element2, ..., elementN).collect(Collectors.toCollection(ArrayList::new));>
1. Exemples:
Java
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> > }> }> |
>
>Sortir
[Geeks, For, Geeks]>