본문 바로가기
> JAVA/Class 클래스

[Java] 2019.09- [Class] ArrayList<E>

by bky373 2020. 9. 20.

ArrayList

알고리즘 문제에서 자주 사용되는 클래스이다. 사실 어딜가나 여기저기 많이 사용될 거 같다.

아래서 내용을 살펴보고 정리해보겠다.

급하다면 아래 요약 부분을 참고하자!

생성자나 메소드 등에 대한 설명은 뒤로 하고

ArrayList클래스가 본질적으로 가지고 있는 속성이나 특징 등을 살펴보려 한다.

public class ArrayList extends AbstractList****

implements List**,** RandomAccess**,** Cloneable**,** Serializable

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null.

> List 인터페이스를 구현한 것.사이즈 크기 조절이 가능한 배열이다. 대학교에서 전공선택 과목을 고르듯 클래스는 선택적인 리스트 작업이 모두 가능하며, null을 포함한 모든 요소로 구현가능하다.

In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)

> List 인터페이스를 구현하는 것과 더불어, 클래스는 배열의 크기를 조작하는 메소드를 제공한다. (동기화되어있지 않다는 점만 빼면 거의 Vector와 동일하다.)

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time.

> size, isEmpty, get, set, iterator, 그리고 listIterator 작업이 일정한 시간에 처리된다.

The add operation runs inamortized constant time, that is, adding n elements requires O(n) time.

> add 작업은 상각 상수시간으로 돌아가는데, 원소를 n만큼 추가하면 O(n) 시간이 걸리기 때문이다.

All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.

> 다른 모든 작업들은 선형시간으로 동작한다(대략 말해서). LinkedList 구현과 비교할 때 상수 요소가 낮다.

Each ArrayList instance has acapacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.

> 클래스의 각 객체는 용량을 갖는다. 이는 리스트 내부에 요소들을 저장하는 데 사용되는 배열의 크기이다. 그 크기는 적어도 항상 리스트 크기만큼 크다. ArrayList에 요소들이 추가될 때 용량은 자동적으로 커진다.

The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

> 크기성장 정책의 세부 사항은 한 원소를 더하는 것이 일정한 상각시간 비용을 갖는다는 사실 말고는 명시되지 않는다.

An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.

> 애플리케이션은 ensureCapacity 작업을 통해 더 많은 수의 원소들을 추가하지 않고도 ArrayList의 용량을 늘릴 수 있다. 이것으로 증가 재할당량을 줄일 수 있다.

Note that this implementation is not synchronized.**If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it_must_be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.)**

> 이 구현은 동기화되지 않는다! 만약 멀티스레드가 ArrayList객체에 동시에 접근하고, 스레드들중 하나(또는 그 이상)가 list를 구조적으로 수정하면, 외부적으로 동기화를 시켜놔야 한다. (구조적 수정이란 add나 delete, 또는 배킹배열의 크기를 명시적으로 재조정하는 것 등을 말한다. 단순히 한 원소의 값을 설정하는 것이 아니다.)

This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the**Collections.synchronizedList**method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

List list = Collections.synchronizedList(new ArrayList(...));

> 외부적인 동기화는 대개 list(를 자연적으로 캡슐화하는)객체를 동기화하는 것으로 가능하다. 만약 그런 객체가 존재하지 않으면 list는 반드시 Collections.synchromizedList 메소드가 랩핑해야 한다. list에 우연한 비동기화된 접근이 발생하는 것을 막기 위해서라면 작성시 쓰는 것이 가장 좋다.

The iterators returned by this class's**iteratorandlistIterator**methods arefail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own**removeoraddmethods, the iterator will throw aConcurrentModificationException**.

> 클래스의 iterator와 listIteraor 메소드가 반환하는 iterator들은 fail-fast방식을 취한다. 만약 iterator가 만들어진 후, iterator 고유의 remove나 add 메소드를 통한 것이 아닌 어떤 방법으로라도 list가 구조적으로 변경되는 경우엔 언제든지 ConcurrentModificationException 에러를 던진다.

Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

> 그래서 동시 수정의 경우에, iterator는 제멋대로 위험을 감수하고, 미래에 불확정한 시간에, 불확정한 행동을 감수하는 대신 빠르고 깨끗하게 실패 결론을 내린다.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification.

> fail-fast 방식이 그대로 보장되지 않을 수 있음에 유의해야 한다. 일반적으로 비동기화된 수정 작업이 동시에 진행되는 경우엔 엄격히 보장되지 않는다.

Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness:the fail-fast behavior of iterators should be used only to detect bugs.

> Fail-fast iterator는 최선을 다해 ConcurrentModificationException을 던진다. 그래서 이런 예외에 의존해 프로그램을 작성하는 것은 잘못된 것이다. fail-fast 방식은 버그를 감지하는 데에만 사용되어야 한다.

This class is a member of the**Java Collections Framework**.

> ArrayList 클래스는 Java Collections Framework의 멤버 중 하나다.

Since:

1.2

See Also:

Collection**,List,LinkedList,Vector,**Serialized Form




*** 요약 ***

ArrayList는 말그대로 List 기능을 수행하는 "배열"이며, 크기 조절이 가능하다.

여기까진 vector와 동일한데, 한 가지 다른 점이 있다면

*동기화 되어 있지 않다는 점이다.

iterator를 사용할 경우 iterator내부 메소드 외의 방법으로

list 수정시 에러가 발생할 수 있다.

* 동기화하려면 외부에 명시적으로 동기화를 해주어야 한다.


출처 : https://docs.oracle.com/en/java/javase/12/docs/api

'> JAVA > Class 클래스' 카테고리의 다른 글

[Java] 2019.09- [Class] BufferedReader  (0) 2020.09.22
[Java] 2019.09- [Class] StringBuffer  (0) 2020.09.20
[Java] 2019.09- [Class] StringBuilder  (0) 2020.09.20

댓글