개발공부/Java
(10) List(ArrayList)
klyhyeon
2020. 7. 13. 20:08
728x90
1. List 메서드
List<String> example = new ArrayList<>();
- example.add("A") : 1이 자동으로 채워진다.
- example.add("B", 0) : index 0의 위치에 B가 저장된다.
- example.size() : example List 배열 길이
- example.set(1, "C") : index 1의 위치에 C가 저장된다.
- example.remove(2) : index 2의 위치 값 삭제
> det. 나아가서 List의 모든 값을 지울 땐 foreach를 써서 배열 담는 값을 메서드의 매개변수로 지정하면 된다.
- example.contains("D") : List에 D가 존재하는 지 boolean으로 나타냄
- example.clear() : List 내 모든 값 삭제
- example.isEmpty() : List가 비었는지 boolean으로 나타냄
- Collections.sort(scores) : scores(Integer) List 오름차순 정렬
ArrayList<Integer> scores = new ArrayList<>();
scores.add(90);
scores.add(80);
scores.add(70);
scores.add(60);