Zainicjuj ArrayList w Javie
Lista tablic jest częścią ramy kolekcji i jest obecny w pakiecie Java.util. Zapewnia nam dynamiczne tablice w Javie. Chociaż może być wolniejszy niż standardowe tablice, ale może być pomocny w programach, w których potrzebne jest wiele manipulacji tablicą.
- ArrayList dziedziczy klasę AbstractList i implementuje interfejs List.
- ArrayList jest inicjowana przez rozmiar, jednak rozmiar może wzrosnąć, jeśli kolekcja się powiększy lub zmniejszy, jeśli obiekty zostaną usunięte z kolekcji.
- Java ArrayList pozwala nam na losowy dostęp do listy.
- ArrayList nie można używać dla typów pierwotnych, takich jak int, char itp. W takich przypadkach potrzebujemy klasy opakowującej (szczegóły znajdziesz w tym artykule).
- ArrayList w Javie można postrzegać jako podobną do wektor w C++ .
Poniżej znajdują się różne metody inicjowania ArrayList w Javie:
Inicjalizacja za pomocą add()
- Składnia:
ArrayList str = new ArrayList(); str.add('Geeks'); str.add('for'); str.add('Geeks'); - Przykłady:
Jawa
// 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);> > }> }> |
Wyjście:
ArrayList : [Geeks, for, Geeks]
- Przykłady: użycie skróconej wersji tej metody
Jawa
// 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);> > }> }> |
Wyjście:
ArrayList : [Geeks, for, Geeks]
Inicjalizacja za pomocą asList()
- Składnia:
ArrayList obj = new ArrayList( Arrays.asList(Obj A, Obj B, Obj C, ....so on));
- Przykłady:
Jawa
// 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);> > }> }> |
Wyjście:
ArrayList : [Geeks, for, Geeks]
Inicjalizacja metodą List.of().
- Składnia:
List obj = new ArrayList( List.of(Obj A, Obj B, Obj C, ....so on));
- Przykłady:
Jawa
// 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);> > }> }> |
Wyjście:
ArrayList : [Geeks, for, Geeks]
Inicjalizacja przy użyciu innej kolekcji
- Składnia:
List gfg = new ArrayList(collection);
- Przykłady:
Jawa
// 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);> > }> }> |
Wyjście:
ArrayList : [1, 2, 3, 4, 5]
Inicjalizacja przy użyciu metod stream() icollect().
1. Składnia:
ArrayList listName = Stream.of(element1, element2, ..., elementN).collect(Collectors.toCollection(ArrayList::new));
1. Przykłady:
Jawa
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> > }> }> |
Wyjście
[Geeks, For, Geeks]