List 인터페이스 : 인덱스 이용, 데이터 중복 가능
- Vector
- ArrayList
- LinkedList
Map 인터페이스 : key를 이용, key는 중복 불가, 데이터는 중복 가능
- HashMap
ArrayList 클래스
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;
public class No14_List {
public static void main(String[] args) {
// 객체생성
ArrayList<String> StrList = new ArrayList<>();
Vector<Integer> vtr = new Vector<>();
LinkedList<String> StrLinked = new LinkedList<>();
// 데이터추가
StrList.add("일어나서 씻기");
StrList.add("아침먹기");
StrList.add("공부하기");
System.out.println("StrList.size: "+StrList.size()); // 사이즈
System.out.println("StrList: "+StrList);
StrList.add(1, "운동하기"); // 원하는 인덱스에 데이터추가
System.out.println("StrList: "+StrList);
StrList.set(1, "요가하기"); // 데이터 재설정
System.out.println("StrList: "+StrList);
// 데이터 추출
String str1 = StrList.get(3);
System.out.println("StrList.get(3): "+str1);
int index1 = StrList.indexOf("요가하기");
System.out.println("StrList.indexOf(\"요가하기\"): "+index1);
// 데이터 존재여부
boolean a = StrList.contains("공부하기");
System.out.println("StrList.contains(\"공부하기\"): "+a);
boolean b = StrList.isEmpty();
System.out.println("StrList.isEmpty() : "+ b);
// 데이터 제거 & 전체제거
str1 = StrList.remove(0);
System.out.println("StrList.remove(0): "+str1);
System.out.println("StrList: "+StrList);
// 데이터 전체제거
StrList.clear();
System.out.println("StrList: "+StrList);
b = StrList.isEmpty();
System.out.println("StrList.isEmpty() : "+ b);
}
}
HashMap 클래스
import java.util.HashMap;
public class No14_Map {
public static void main(String[] args) {
// HashMap객체생성
HashMap<Integer, String> map = new HashMap<>();
// 데이터 추가
map.put(6, "사원 김철수");
map.put(7, "사원 박명수");
map.put(8, "사원 유재석");
System.out.println("map: "+map);
System.out.println("map.size(): "+map.size());
// 데이터 교체
map.put(6, "대리 김철수");
System.out.println("map: "+map);
// 데이터 추출
String str = map.get(6);
System.out.println("map.get(6): "+str);
// 데이터 제거
map.remove(7);
System.out.println("map: "+map);
// 특정 데이터 포함유무
boolean a = map.containsKey(7);
System.out.println("map.containsKey(7): "+a);
a = map.containsValue("사원 유재석");
System.out.println("map.containsValue(\"사원 유재석\"): "+a);
// 데이터 전체제거
map.clear();
System.out.println("map: "+map);
// 데이터 유무
a = map.isEmpty();
System.out.println("map.isEmpty(): "+a);
}
}
'Java' 카테고리의 다른 글
[Java] 데이터 입출력(1) FileInputStream, DataInputStream, FileOutputStream, DataOutputStream (0) | 2021.08.09 |
---|---|
[Java] 예외처리 (0) | 2021.08.02 |
[Java] StringBuffer, StringBuilder (0) | 2021.08.01 |
[Java] 람다식 (0) | 2021.08.01 |
[Java] 추상클래스 (0) | 2021.07.29 |