Java에서 ArrayList 초기화
배열목록 의 일부입니다 수집 프레임워크 java.util 패키지에 있습니다. 이는 Java로 동적 배열을 제공합니다. 하지만 표준 배열보다 느릴 수 있지만 배열에서 많은 조작이 필요한 프로그램에서는 도움이 될 수 있습니다.
- ArrayList는 AbstractList 클래스를 상속하고 List 인터페이스를 구현합니다.
- ArrayList는 크기에 따라 초기화되지만 컬렉션이 커지면 크기가 늘어나고 컬렉션에서 개체가 제거되면 축소될 수 있습니다.
- Java ArrayList를 사용하면 목록에 무작위로 액세스할 수 있습니다.
- ArrayList는 int, char 등과 같은 기본 유형에 사용할 수 없습니다. 이러한 경우에는 래퍼 클래스가 필요합니다(자세한 내용은 이 항목을 참조하세요).
- Java의 ArrayList는 다음과 유사하게 볼 수 있습니다. C++의 벡터 .
다음은 Java에서 ArrayList를 초기화하는 다양한 방법입니다.
add()를 사용한 초기화
- 통사론:
ArrayList str = new ArrayList(); str.add('Geeks'); str.add('for'); str.add('Geeks'); - 예:
자바
// 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);> > }> }> |
산출:
ArrayList : [Geeks, for, Geeks]
- 예: 이 방법의 단축 버전 사용
자바
// 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);> > }> }> |
산출:
ArrayList : [Geeks, for, Geeks]
asList()를 사용한 초기화
- 통사론:
ArrayList obj = new ArrayList( Arrays.asList(Obj A, Obj B, Obj C, ....so on));
- 예:
자바
// 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);> > }> }> |
산출:
ArrayList : [Geeks, for, Geeks]
List.of() 메소드를 사용한 초기화
- 통사론:
List obj = new ArrayList( List.of(Obj A, Obj B, Obj C, ....so on));
- 예:
자바
// 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);> > }> }> |
산출:
ArrayList : [Geeks, for, Geeks]
다른 컬렉션을 사용한 초기화
- 통사론:
List gfg = new ArrayList(collection);
- 예:
자바
// 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);> > }> }> |
산출:
ArrayList : [1, 2, 3, 4, 5]
stream() 및 Collect() 메서드를 사용한 초기화
1. 통사론:
ArrayList listName = Stream.of(element1, element2, ..., elementN).collect(Collectors.toCollection(ArrayList::new));
1. 예:
자바
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> > }> }> |
산출
[Geeks, For, Geeks]