일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- javascript
- 기본코드
- html
- java입문
- java
- Git이해하기
- MySQL
- Eclipse
- Git알아보기
- 이클립스
- 필드
- 점프 투 파이썬
- jdbc
- 버튼페이지이동
- js
- java기초
- 파일이동버튼
- form
- object
- emmet
- arraylist
- Collection
- 맨땅에 해딩
- 컬렉션프레임워크
- 배열
- CSS
- list
- 오버라이딩
- 데이터베이스연동
- cmd
- Today
- Total
단단히
컬렉션 프레임워크( Collection Framework / C.F)_List.Vocter 본문
[Voctor]
Voctor는 다른 컬렉션 프레임워크 중 가장 먼저 등장한 클래스이다.
ArrayList와 동일한 내부 구조를 가진다.(배열을 가지고 있으며 순서대로 값들을 저장한다.)
ArrayList와 다른 부분이 있는데, Voctor는 동기화된 메서드로 구성되어있다. 그래서 멀티 스레드가 동시에 메서드들을 실행할 수 없다. 하나의 스레드의 실행을 완료하고 나서야 다른 스레드를 실행할 수 있다.
그래서 멀티 스레드 환경에서 안전하게 객체를 추가, 삭제할 수 있다.
이것을 스레드가 안전하다(Thread Safe)라고 한다.
[Voctor 생성]
List<E> list = new Voctor <E>();
package pack_pratice;
import java.util.List;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
List<LolChampion> list = new Vector<LolChampion>();
// Vector도 ArrayList와 같이 add메서드를 사용여 추가한다..
list.add(new LolChampion("Neeko","Pop Blossom"));
list.add(new LolChampion("Bard","\tTempered Fate"));
list.add(new LolChampion("Pantheon","Grand Starfall"));
list.add(new LolChampion("Veigar","Primordial Burst"));
list.add(new LolChampion("LeBlanc","Mimic"));
list.add(new LolChampion("Sona","\tCrescendo"));
//remove를 사용하여 삭제한다.(뒤쪽 인덱스가 한개씩 당겨진다.)
list.remove(2);
for (int i=0 ; i<list.size() ; i++) {
LolChampion lolCham = list.get(i);
System.out.println(lolCham.name + "\t\t" + lolCham.skillR);
}
}
public static class LolChampion{
String name;
String skillR;
public LolChampion(String name, String skillR) {
this.name=name;
this.skillR=skillR;
}
}
}
[마주한 오류]
No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main).
계속해서 컴파일 오류가 발생했다.
static함수에서 참조하려는 클래스에 접근을 하지 못한다는 오류이다. 해결방법은 하위 클래스에 static 키워드를 붙여주면 된다.
*** static 키워드에 대해 한 번 더 공부해야겠다. ***
[Vector 전체 값 확인]
package pack_pratice;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
List<String> list = new Vector<>(Arrays.asList("Neeko","Bard","Zoe","Aurelion Sol"));
for (int i=0 ; i<list.size() ; i++) {
String lolCham = list.get(i);
System.out.print(lolCham +"\t");
}
//향상된 for문 (for-each loop)
for (String lolCham : list) {
System.out.print(lolCham+"\t");
//순방향 출력
}
//iterator
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next()+"\t");
}
//listIterator **역방향 출력**
ListIterator<String> listIterator = list.listIterator(list.size());
while (listIterator.hasPrevious()) {
System.out.print(listIterator.previous() + "\t");
}
}
}
[특징]
Collection 프레임워크에 없는 메서드를 사용할 수 있다. 항상 동기화되어있기 때문에 안전성이 높지만 ArrayList와 비교하면 성능(추가, 검색, 삭제)이 떨어진다. 그렇기에 스레드를 사용하지 않을 경우에는 ArrayList를 사용하는 게 더 좋다.
'Java > 개념 정리' 카테고리의 다른 글
컬렉션 프레임워크( Collection Framework / C.F)_List.LinkedList (0) | 2022.07.20 |
---|---|
빈(Bean) 클래스_ Getter와 Setter (0) | 2022.07.10 |
컬렉션 프레임워크_List (0) | 2022.06.30 |
컬렉션 프레임워크 (0) | 2022.06.20 |
배열 _ 다차원 배열 (0) | 2022.06.15 |