Java
[exam14] List
감자탈출기
2021. 9. 11. 20:29
List 기본 활용
public class Sample1 {
public static void main(String[] args) {
// int[] : 정수 데이터를 저장할 배열 공간 지정
//List<Integer> iList = new ArrayList<Integer>(); //다똑같이작동함
//List<Integer> iList = new Vector<Integer>(); //다똑같이작동함
List<Integer> iList = new LinkedList<Integer>(); //다똑같이작동함
// <Object Type> : 제네릭(Generic)으로 컬렉션에 저장할 데이터 객체 타입을 지정하기 위해 사용
//추가
iList.add(10); // 먼저 넣은 순서대로 추가 됨
System.out.println(iList); // toString 안 써도 알아서 보기 좋게 출력해 줌
iList.add(20);
System.out.println(iList);
iList.add(30);
System.out.println(iList);
iList.add(1, 15); // 1번 인덱스 자리에 추가할 거다!
System.out.println("인덱스 판별 : " +iList);
iList.add(3, 15); // 3번 인덱스 자리에 추가
System.out.println(iList);
//System.out.println(iList.hashCode()); 번지 같은 애
List<Integer> addList = new ArrayList<Integer>();
addList.addAll(iList); //iList가 가진 내용을 addList에 모두 넣어라
System.out.println(addList);
// 수정
iList.set(1, 16); // 1번 인덱스 내용을 16으로 변경하겠다.
System.out.println(iList);
iList.set(3, 26); // 3번 인덱스 내용을 26으로 변경하겠다.
System.out.println(iList);
// 검색 // 가지고 있냐 없냐
boolean resBool = iList.contains(20); // boolean으로 반환해주기 때문에
System.out.println(resBool);
resBool = iList.contains(25);
System.out.println(resBool);
int resInt = iList.indexOf(20); // 몇번 인덱스에 있냐
System.out.println(resInt);
resInt = iList.indexOf(25);
System.out.println(resInt); // 없다면 -1 반환함
// int resInt = arr[0]; // 원래 배열에 있는 값 반환하던 식
resInt = iList.get(0); // 인덱스 번호 범위 벗어나면 안 됨!
System.out.println(resInt);
// 전체 탐색 방법
for(int i = 0; i < iList.size(); i++) { //얘는 길이가 사이즈임!
System.out.println("반복문으로 전체 탐색 -> " + iList.get(i)); //인덱스 쓴 위치 기억
}
for(Integer i: iList) { // for(int i: iList) 인트로 써도 상관 없음
System.out.println("for each 문을 사용한 전체 탐색 -> " + i);
}
// 삭제
resInt = iList.remove(0); // remove(index)로 삭제 된 데이터 특이하게 int로 반환이 됨. 별도 활용 가능
System.out.println(iList + " | " + resInt);
resBool = iList.remove(new Integer(20)); // remove(Object) 쓰기 위해 (얘는 불린 반환)
System.out.println(iList + " | " + resBool); // 20 이라는 정수를 인티저 객체로 만든 것
// iList에 객체 값이 있으면 삭제하는 것
resBool = iList.isEmpty(); // 비워져 있냐?
System.out.println(resBool); // 아뇨 false
iList.clear(); // 전체 삭제
System.out.println(iList);
resBool = iList.isEmpty(); // 비워져 있냐?
System.out.println(resBool); // 네 true
List 정렬 방법
//정렬
iList.add(13); iList.add(15); iList.add(12); iList.add(11);
System.out.println(iList);
//순서 뒤집기
Collections.reverse(iList);
System.out.println(iList);
//오름차순 정렬 //sort는 List로만 가능 Set으로 안 됨!
Collections.sort(iList);
System.out.println(iList);
//오름차순 정렬 후 reverse 하면 내림차순 정렬이 됨
Collections.reverse(iList);
System.out.println(iList);
// 내림차순 정렬 인티저는 그냥 reverse 방법 쓰시고 객체정렬은 컴페어 필요하므로 아래쪽에 스튜던트 보기
//sort(iList, new Comparator<Integer>() 이게
//List를 new Comparator<Integer>(){}가지고 배열한다는 뜻
Collections.sort(iList, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if(o1 > o2) {
return -1;
} else if(o1 < o2) {
return 1;
}
return 0;
}
});
System.out.println(iList);
//객체 정렬 중요
List<Student1> sList = new ArrayList<Student1>();
sList.add(new Student1("홍길동", 86));
sList.add(new Student1("박수연", 76));
sList.add(new Student1("금장현", 86));
sList.add(new Student1("이정태", 76));
sList.add(new Student1("김라현", 92));
sList.add(new Student1("장지원", 82));
Collections.sort(sList, new Comparator<Student1>() {
@Override
public int compare(Student1 s1, Student1 s2) { //반환타입 int로 맞춰줘야함
// 내림차순 정렬 //Student 아님! int임
if(s1.getScore() > s2.getScore()) {
return -1;
} else if(s1.getScore() < s2.getScore()) {
return 1;
}
return 0;
}
});
Collections.sort(sList, new Comparator<Student1>() {
@Override
public int compare(Student1 s1, Student1 s2) {
// 오름차순 정렬
if(s1.getName().compareTo(s2.getName()) < 0) {
return -1; //ㄴ이름은 등호 비교 안 되므로 compareTo 써야하고 인트 반환해주니까 0과 비교해야 함
} else if(s1.getName().compareTo(s2.getName()) > 0) {
return 1;
}
return 0;
}
});
Collections.sort(sList, new Comparator<Student1>() {
@Override
public int compare(Student1 s1, Student1 s2) {
// 점수에 대해서는 내림차순
// 만약 점수가 같으면 이름으로 오름차순
int v1 = s1.getScore() - s2.getScore();
int v2 = s1.getName().compareTo(s2.getName());
if(v1 > 0) {
return -1;
} else if(v1 < 0) {
return 1;
}
if (v2 < 0) {
return -1;
} else if (v2 > 0) {
return 1;
}
return 0;
}
});
for(Student1 s: sList) {
System.out.println(s);
}
}