CRUD, LinkedList
import java.util.Iterator;
import java.util.LinkedList;
public class Day2 {
public static void main(String args[]) {
LinkedList<Integer> list = new LinkedList<Integer>();
// 첫번째 출력 방식
for (int i = 0; i < 10; i++) {
list.add(i);
System.out.println(list.get(i));
}
// 두번째 출력 방식
System.out.println(list); // 배열 형식으로 전부 출력
// 세번째 출력 방식
for (int a : list)
System.out.print(a + " ");
System.out.println();
// 네번째 출력 방식
Iterator<Integer> it = list.iterator();
while(it.hasNext()) {
System.out.print(it.next() + " ");
}
}
}
import java.util.LinkedList;
class Apple {
int count;
int age;
Apple(int count, int age) {
this.count = count;
this.age = age;
}
void show() {
System.out.println(count + " " + age);
}
}
public class Day2 {
public static void main(String args[]) {
LinkedList<Apple> list = new LinkedList<Apple>();
Apple a = new Apple(10, 20);
list.add(a);
Apple b = new Apple(30, 40);
list.add(b);
list.add(new Apple(40, 50));
for (Apple apple : list) {
apple.show();
}
}
}
import java.util.LinkedList;
class Apple {
int count;
int age;
Apple(int count, int age) {
this.count = count;
this.age = age;
}
void show() {
System.out.println(count + " " + age);
}
}
public class Day2 {
public static void main(String args[]) {
LinkedList<Apple> list = new LinkedList<Apple>();
for (int i = 0; i < 10; i++) {
list.add(new Apple(i, i * 10));
}
System.out.println("-------after create-------");
for (Apple apple : list) {
apple.show();
}
System.out.println("-------after update-------");
list.set(4, new Apple(99, 990));
for (Apple apple : list)
apple.show();
System.out.println("-------after delete-------");
list.remove(4); // index 4가 뭔 지 알고 삭제? 무의미한 코드
for (Apple apple : list)
apple.show();
// age가 50을 가진 Apple 삭제하고 싶어! 검색 후 삭제
for (int i = 0; i < list.size(); i++) {
Apple apple = list.get(i);
if (apple.age == 50) {
list.remove(i);
break;
}
}
// count가 1의 배수인 Apple만 삭제하고 싶어!
System.out.println("-------delete-----------");
for (int i = 0; i < list.size();) {
Apple apple = list.get(i);
if ((apple.count % 1) == 0) {
list.remove(i);
} else i++;
}
for (Apple apple : list)
apple.show();
}
}