package demo.list;
import java.util.ArrayList;
public class ArrayListDemo1 {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
// 객체 저장하기
// boolean add(E e)
names.add("홍길동"); //names[0] = "홍길동";
names.add("김유신"); //names[1] = "김유신";
names.add("강감찬"); //names[2] = "강감찬";
names.add("이순신"); //names[3] = "이순신";
names.add("유관순"); //names[4] = "유관순";
names.add("유관순"); //names[4] = "유관순";
names.add("유관순"); //names[4] = "유관순";
System.out.println(names);
// 지정된 위치의 객체 삭제
// E remove(int index)
names.remove(2); // 강감찬 삭제
// 지정된 객체를 삭제
// boolean remove(Object o)
names.remove("유관순"); // 유관순 삭제
// 지정된 인덱스에 저장된 객체 꺼내기
// E get(int index)
String value1 = names.get(0);
System.out.println("0번째 저장된 객체: " + value1);
// ArrayList에 저장된 모든 객체 꺼내기
// enhanced for문 사용
for (String x : names) {
System.out.println(x);
}
// int size()
// 저장된 객체의 개수를 반환
int len = names.size();
System.out.println("저장된 객체의 개수: " + len);
// void clear()
// 저장된 객체 전체 삭제
names.clear();
// boolean isEmpty()
boolean empty = names.isEmpty();
System.out.println("비어있는가? " + empty);
}
}
package demo.list;
import java.util.ArrayList;
public class ArrayListDemo2 {
public static void main(String[] args) {
// 기본 자료형을 저장하는 ArrayList
// 기본 자료형을 저장할 때는 제네릭 타입을 Wrapper 클래스 타입으로 지정한다.
// int --> Integer, double --> Double, long --> Long, ...
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(new Integer(20));
numbers.add(40); // 오토 박싱 --> numbers.add(new Integer(40))
for (Integer x : numbers) {
System.out.println(x);
}
for (int x : numbers) {
System.out.println(x);
}
ArrayList<Double> scores = new ArrayList<Double>();
}
}
package demo.list;
import java.util.ArrayList;
public class ArrayListDemo3 {
public static void main(String[] args) {
// 객체를 저장하는 ArrayList
ArrayList<Book> books = new ArrayList<Book>();
Book book1 = new Book(100, "이것이 자바다", "신용권", "한빛미디어", 30000);
Book book2 = new Book(200, "데이터베이스 개론", "홍길동", "한빛미디어", 29000);
Book book3 = new Book(300, "자바 디자인 패턴", "켄트벡", "인사이트", 32000);
Book book4 = new Book(400, "인사이트 자바스크립트", "김유신", "한빛미디어", 27000);
books.add(book1);
books.add(book2);
books.add(book3);
books.add(book4);
books.add(new Book(500, "스프링 3.0", "이일민", "에이콘출판사", 75000));
System.out.println(books);
int total = 0;
for (Book book : books) {
System.out.println("제목 : " + book.getTitle());
System.out.println("가격 : " + book.getPrice());
total += book.getPrice();
}
System.out.println("전체 가격 : " + total);
}
}
package demo.list;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo4 {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("홍길동");
names.add("김유신");
names.add("강감찬");
names.add("이순신");
names.add("이성계");
names.add("김구");
names.add("김좌진");
for (String name : names) {
System.out.println(name);
}
System.out.println();
for (int i=0; i<names.size(); i++) {
String name = names.get(i);
System.out.println(name);
}
System.out.println();
/*
* Iterator<E>
* 콜렉션(List 및 Set)의 요소를 추출, 삭제할 때 사용되는 객체다.
* 주요 메소드
* boolean hasNext() - 추출할 객체가 남아있으면 true를 반환한다.
* E next() - 객체를 하나 꺼내서 반환한다.
* void remove() - 객체를 삭제한다.
*/
Iterator<String> it = names.iterator();
while (it.hasNext()) {
String name = it.next();
System.out.println(name);
if (name.startsWith("김")) {
it.remove();
}
}
System.out.println(names);
}
}
package demo.list;
public class Book {
private int no;
private String title;
private String author;
private String publisher;
private int price;
public Book() {}
public Book(int no, String title, String author, String publisher, int price) {
super();
this.no = no;
this.title = title;
this.author = author;
this.publisher = publisher;
this.price = price;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Book [no=" + no + ", title=" + title + ", author=" + author + ", publisher=" + publisher + ", price="
+ price + "]";
}
}