ArrayList와 List의 차이
Arraylist
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including 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.
List
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
ArrayList<Object> arraylist = new ArrayList<>();
List<Object> list = new ArrayList<>();
위 두 코드는 같은 결과를 도출한다 그렇다면 둘의 차이는 뭘까?
List는 인터페이스이고 ArrayList는 List 인터페이스를 구현한 클래스들 중 하나이다.
데이터의 용도에 따라 빠른 탐색을 위해 ArrayList를 사용할 때도 있고, 빠른 삽입과 삭제를 위해 LikedList를 사용해야 하는 경우도 있다.
List는 ArrayList와 LinkedList의 인터페이스이기 때문에 다형성의 효과로 List로 클래스를 생성하면 여러 구현 클래스들로도 사용이 가능하다.
→ List 리모컨으로 생성하면 ArrayList, LinkedList 등의 티비들을 전부 조종 가능!
아래와 같이 선언해뒀던 List를
List list = new List();
삽입과 삭제를 위해 LinkedList로 바꿔도 문제없이 사용 가능하다.
List list = new LinkedList();
'JAVA > Java 문법' 카테고리의 다른 글
Wrapper Class (0) | 2022.02.15 |
---|