wisePocket

[JAVA] 기초문법 - 자료구조 컬렉션 ArrayList 생성과 메서드 기능 이용 연습 - 21 본문

Java & Algorithm/Java

[JAVA] 기초문법 - 자료구조 컬렉션 ArrayList 생성과 메서드 기능 이용 연습 - 21

ohnyong 2023. 7. 27. 19:43

Collection - ArrayList

ArrayList를 복습하고자 한다.

  • ArrayList - 순서가 있는 데이터의 집합, 중복을 허용
  • Array와의 차이점은 Array는 생성시 길이(크기)(length)를 지정하는 정적 배열인데,
  • ArrayList는 가변적으로 늘어난다 동적 배열이다.(연속된 공간 요청-생성)

  • element, item이 혼용되어 사용되어 공식 홈페이지를 참고하니 각 자료구조마다 명칭을 다르게 부르는 것을 보았다.
  • ArrayList, LinkedList, Queue, Set -> element (리스트 리스트 set et element)
  • Stack -> item (쌓이는거 통과하는거 물건이니까 아이템)
  • Map -> Key, Value 키맵 키맵 맵 키 밸류
  • 항상 공식 문서를 확인하고 정확한 명칭을 기억하려 노력하자
  • https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
 

ArrayList (Java Platform SE 8 )

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

docs.oracle.com



1. 기본 생성법

E - the type of elements maintained by this set
ArrayList<E> name = new ArrayList<E>();

        ArrayList<Integer> intList = new ArrayList<Integer>();

 

2. ArrayList의 메서드를 사용

.add(E e) : element 추가
        //추가(c)
        //add(element) 메서드로 생성한 List에 element를 넣어보자.
        intList.add(99);
        intList.add(15);
        intList.add(3);
        //->[{99}, {15}, {3}]

.get(int idx) : idx의 element 찾기

        //get(index) 메서드로 List의 index값을 통해서 List에 추가한 element를 찾아보자.
        System.out.println(intList.get(2));
        //->{3}
        //toString() 메서드로 List 전체 element들을 조회 할 수 있다.
        System.out.println(intList.toString());


.set(int idx, E element) : idx에 element 수정

        //set(index, element) 메서드로 List의 대상 index의 element를 입력한 element로 변경하자.
        intList.set(1, 10);
        //->[{99},  {10}  , {3}]


.remove(int idx) : idx의 element 삭제

        //삭제(d)
        //remove(index) 메서드로 List의 index값을 통해서 element를 삭제하자.
        intList.remove(2);
        System.out.println(intList.toString());
        //->[{99}, {10}, {}]


.clear() : element 모두삭제

        //clear() 메서드로 List의 모든 element를 삭제하자.
        intList.clear();
        System.out.println(intList.toString());​