ArrayList는 Java의 컬렉션 프레임워크에 포함된 List 인터페이스의 구현으로, 런타임 시 배열의 크기를 동적으로 늘릴 수 있습니다. 이 클래스는 내부적으로 java.util.package에서 사용할 수 있으며 배열 데이터 구조를 사용합니다. ArrayList는 Java에서 래퍼 클래스만 허용하며, 클래스는 사용자가 정의합니다.
무료 소프트웨어 개발 과정 시작
웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등
구문:
ArrayList<t> list = new ArrayList();</t>
List<t> list = new ArrayList();</t>
ArrayList의 인스턴스를 직접 사용하거나 이를 List 참조에 할당할 수 있습니다.
구성자:
배열 목록에는 다음과 같은 세 가지 생성자가 있습니다.
- ArrayList(intinitialCapacity): 여기서 배열 목록의 초기 길이를 지정할 수 있습니다. 하지만 크기가 언급된 초기 용량을 초과하면 ArrayList 클래스가 이를 처리합니다.
- ArrayList(): 이를 통해 초기 용량 없이 빈 목록을 생성할 수 있으므로 이 경우 기본 초기 용량은 10이 됩니다.
- ArrayList(Collection extends E> c): 컬렉션 목록입니다.
Java ArrayList 클래스의 메서드
다음은 Java ArrayList 클래스의 메소드입니다.
- void trimToSize(): This method will trim the list to the current list size.
- Object[] toArray(): Return the array of object.
- boolean remove(Object o): This method is used to remove the object, but it will remove the first occurrence as the list contain duplicate.
- boolean removeAll(Collection> c): This method is used to remove all the elements of the list.
- Boolean removeIf(Predicate super E> filter): This method is used to remove the predicate passed. Used as a filter.
- Add (E e): This method s used to add elements to the list.
- Void add(int index, E element): It takes two-parameter and adds elements t the specific index we mentioned.
- Boolean addAll(Collection extends E> c): This takes a list as an argument and adds all the elements to the end of the current list.
- boolean addAll(int index, Collection extends E> c): This method adds all the elements to the current list at the specified index we pass.
- Get (int index): This method is used to get the element. It will return the element present at the specified position in the list.
- Int indexOf(Object o): This method is used to get the element’s index passed. It will always return the first occurrence of the element into the list because the list can contain duplicate elements.
-
ListIterator
listIterator(int index): This method returns an iterator with the specific index. - Remove (int index): This method removes the element. It will remove the element with the corresponding index passed.
- Protected void removeRange(int fromIndex, int toIndex): This removes the elements from a specified range.
- Boolean retainAll(Collection> c): This method will retain all elements contained in the specified collection.
- Set (int index, E element): This method will set the element to the specified index.
- Void sort(Comparator super E> c): This method is used to sort the collection element.
-
List
subList(int fromIndex, int toIndex): This method will be used to return the sublist from the specified index. - Void clear(): This mentioned is used to clear the elements in the list.
- Object clone(): These methods create a copy of the list.
- Boolean contains(Object o): This method is used to check whether the passing object is present in the list or not.
- Void ensureCapacity(int minCapacity): This method is used to increase the capacity of the array list.
- Boolean isEmpty(): This method is used to check whether the array list is empty or not.
-
Iterator
iterator(): This method returns iterator. - int lastIndexOf(Object o): This method returns the last index of the object passed. If the object does not present in the list, it will return -1.
-
ListIterator
listIterator(): This methods return iterator.
Examples of Java ArrayList Class
Examples of Java ArrayList Class are given below:
1. add an element in ArrayList
The below example will show how to add an element to an array list.
Code:
package com.cont.article; import java.util.ArrayList; public class ArratListTest { public static void main(String[] args) { ArrayList<string> list = new ArrayList(); list.add("abc"); list.add("xyz"); list.add("yyy"); list.add("some name"); list.add("other name"); System.out.println("list is :: " + list); } }</string>
Output:
2. Copying elements of list one to another list
Code:
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<string> list = new ArrayList(); list.add("abc"); list.add("xyz"); list.add("yyy"); list.add("some name"); list.add("other name"); ArrayList<string> list2 = new ArrayList(); list2.addAll(list); System.out.println("Elements in list one : " + list); System.out.println("Elements in list two : " + list2); } }</string></string>
Output:
3. Remove element from ArrayList
Code:
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<string> list = new ArrayList(); list.add("abc"); list.add("xyz"); list.add("yyy"); list.add("some name"); list.add("other name"); System.out.println("Size of list before remove ::" + list.size()); System.out.println("Elements in list are before remove " + list); list.remove(4); System.out.println("Size of list after removinf element :: " +list.size()); System.out.println("Elements in list are after remove" + list); } }</string>
Output:
4. Clear all the elements from ArrayList
Code:
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<string> list = new ArrayList(); list.add("abc"); list.add("xyz"); list.add("yyy"); list.add("some name"); list.add("other name"); list.clear(); System.out.println("Clering all elements of list " +list.size()); } }</string>
Output:
5. Iterate all the elements of ArrayList
Code:
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<string> list = new ArrayList(); list.add("abc"); list.add("xyz"); list.add("yyy"); list.add("some name"); list.add("other name"); System.out.println("Priniting out element."); for (String string : list) { System.out.println("Elemnt in the list is"); System.out.println(string); } } }</string>
Output:
The array list in java does not contain duplicate elements. Also, the insertion order is maintained in a list, which means we put over elements that will generate the output in the same sequence. Some detailed points which need to be remembered for the array list in java are as follows:
It implements various interfaces:
- Serializable,
- Iterable
, - Cloneable,
- Collection
, - List
, - RandomAccess
The class hierarchy is as follows:
java.lang.Object >> java.util.AbstractCollection<e> >> java.util.AbstractList<e> >> java.util.ArrayList<e></e></e></e>
By default, the array list is not synchronized in nature and is not thread-safe, But we can make them synchronized using the collections class synchronized method. The syntax is described below for reference :
List arrList = Collections.synchronizedList (new ArrayList(...));
위 내용은 Java ArrayList 클래스의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

이 기사에서는 Java 프로젝트 관리, 구축 자동화 및 종속성 해상도에 Maven 및 Gradle을 사용하여 접근 방식과 최적화 전략을 비교합니다.

이 기사에서는 Maven 및 Gradle과 같은 도구를 사용하여 적절한 버전 및 종속성 관리로 사용자 정의 Java 라이브러리 (JAR Files)를 작성하고 사용하는 것에 대해 설명합니다.

이 기사는 카페인 및 구아바 캐시를 사용하여 자바에서 다단계 캐싱을 구현하여 응용 프로그램 성능을 향상시키는 것에 대해 설명합니다. 구성 및 퇴거 정책 관리 Best Pra와 함께 설정, 통합 및 성능 이점을 다룹니다.

이 기사는 캐싱 및 게으른 하중과 같은 고급 기능을 사용하여 객체 관계 매핑에 JPA를 사용하는 것에 대해 설명합니다. 잠재적 인 함정을 강조하면서 성능을 최적화하기위한 설정, 엔티티 매핑 및 모범 사례를 다룹니다. [159 문자]

Java의 클래스 로딩에는 부트 스트랩, 확장 및 응용 프로그램 클래스 로더가있는 계층 적 시스템을 사용하여 클래스로드, 링크 및 초기화 클래스가 포함됩니다. 학부모 위임 모델은 핵심 클래스가 먼저로드되어 사용자 정의 클래스 LOA에 영향을 미치도록합니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

mPDF
mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)
