티스토리에 저장했던 글을 옮겼습니다.
https://mrcocoball.tistory.com/73
public class Book {
// field
private String title;
private String author;
// constructor
public Book() {}
public Book(String title, String author) {
this.title = title;
this.author = author;
}
// getter and setter (Resources -> Generate -> getter and setter)
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;
}
// method
public void showInfo() {
System.out.println(title + "," + author);
}
public class BookTest {
public static void main(String[] args) {
Book[] library = new Book[5];
// 기본 자료형 배열과 달리 객체 배열의 경우 생성 시 메모리만 할당 되고 각 요소의 초기값은 null
//객체 생성하여 직접 넣기
library[0] = new Book("태백산맥1", "조정래");
library[1] = new Book("태백산맥2", "조정래");
library[2] = new Book("태백산맥3", "조정래");
library[3] = new Book("태백산맥4", "조정래");
library[4] = new Book("태백산맥5", "조정래");
for (Book book : library) {
System.out.println(book);
book.showInfo();
}
}
}
public class ObjectCopyTest {
public static void main(String[] args) {
Book[] library = new Book[5];
Book[] copyLibrary = new Book[5];
//객체 생성하여 직접 넣기
library[0] = new Book("태백산맥1", "조정래");
library[1] = new Book("태백산맥2", "조정래");
library[2] = new Book("태백산맥3", "조정래");
library[3] = new Book("태백산맥4", "조정래");
library[4] = new Book("태백산맥5", "조정래");
System.arraycopy(library, 0, copyLibrary, 0, 5); // System.arraycopy 로 복사
System.out.println("== library ==");
for (Book book : library) {
System.out.println(book);
book.showInfo();
}
System.out.println("== copyLibrary ==");
for (Book book : copyLibrary) {
System.out.println(book); // 출력 시 복사된 객체들의 주소가 library의 객체 주소와 똑같음
book.showInfo();
}
library[0].setTitle("나목");
library[0].setAuthor("박완서");
System.out.println("======library=========");
for( Book book : library) {
book.showInfo();
}
System.out.println("======copy library=========");
for( Book book : copyLibrary) {
book.showInfo();
}
}
}
public class ObjectCopyTest2 {
public static void main(String[] args) {
Book[] library = new Book[5];
Book[] copyLibrary = new Book[5];
//객체 생성하여 직접 넣기
library[0] = new Book("태백산맥1", "조정래");
library[1] = new Book("태백산맥2", "조정래");
library[2] = new Book("태백산맥3", "조정래");
library[3] = new Book("태백산맥4", "조정래");
library[4] = new Book("태백산맥5", "조정래");
// 객체 생성
copyLibrary[0] = new Book();
copyLibrary[1] = new Book();
copyLibrary[2] = new Book();
copyLibrary[3] = new Book();
copyLibrary[4] = new Book();
// System.arraycopy 메서드가 아닌 직접 반복문으로 요소에 객체 넣기
for(int i = 0; i<library.length; i++) {
copyLibrary[i].setAuthor(library[i].getAuthor()); // library 요소 내 객체의 값을 복사, 직접 주입하여 생성
copyLibrary[i].setTitle(library[i].getTitle()); // library 요소 내 객체의 값을 복사, 직접 주입하여 생성
}
System.out.println("== library ==");
for (Book book : library) {
System.out.println(book);
book.showInfo();
}
System.out.println("== copyLibrary ==");
for (Book book : copyLibrary) {
System.out.println(book); // 출력 시 복사된 객체들의 주소가 library의 객체 주소와 다름
book.showInfo();
}
library[0].setTitle("나목");
library[0].setAuthor("박완서");
System.out.println("======library=========");
for( Book book : library) {
book.showInfo();
}
System.out.println("======copy library=========");
for( Book book : copyLibrary) {
book.showInfo();
}
}
}
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<Book> library = new ArrayList<Book>(); // 최근에는 new ArrayList<>(); 로 해도 되긴 함
library.add(new Book("리코의 벽쿵일기1", "리코")); // add로 추가, 이 때 객체 배열은 객체 생성자로 생성하여 추가
library.add(new Book("리코의 벽쿵일기2", "리코"));
library.add(new Book("리코의 벽쿵일기3", "리코"));
library.add(new Book("리코의 벽쿵일기4", "리코"));
library.add(new Book("리코의 벽쿵일기5", "리코"));
for (int i = 0; i<library.size(); i++) { // length 대신 size()
library.get(i).showInfo(); // i번째의 요소를 get 해서 showInfo()
}
// 상세 기능 잘 모를 때 ArrayList 클릭하여 f1 누르면 각 메소드에 대한 설명 나옴!
}
}
public class StudentTest {
public static void main(String[] args) {
Student studentRico = new Student(1001, "리코");
studentRico.addSubject("국어", 100);
studentRico.addSubject("수학", 50);
Student studentYoshiko = new Student(1002, "요시코");
studentYoshiko.addSubject("국어", 70);
studentYoshiko.addSubject("수학", 85);
studentYoshiko.addSubject("영어", 100);
studentRico.showScoreInfo();
System.out.println("======================");
studentYoshiko.showScoreInfo();
}
}
public class Student {
// field
String studentName;
int number;
ArrayList<Subject> subjectList; // ArrayList
// constructor
Student(int number, String studentName) {
this.number = number;
this.studentName = studentName;
subjectList = new ArrayList<>(); // Student 객체 생성 시 subjectList 생성
}
// method
public void addSubject(String name, int point) { // 학생 별로 수강하는 과목의 이름과 점수를 subjectList에 저장
Subject subject = new Subject(); // Subject 객체 생성
subject.setName(name); // Subject 객체의 과목 이름
subject.setScorePoint(point); // Subject 객체의 점수
subjectList.add(subject); // 위의 subject 객체를 subjectList에 저장
}
public void showScoreInfo() {
int total = 0; // 총합 계산을 위한 로컬 변수
for (Subject subject : subjectList) { // 향상된 for 문, subjectList 내에 있는 모든 요소들에 대해 반복
total += subject.getScorePoint(); // subjectList 내에 있는 과목 점수들 합침
System.out.println(studentName + " 학생의 " + subject.getName() + " 과목의 성적은 " + subject.getScorePoint() + "입니다.");
}
System.out.println(studentName + " 학생의 총점은 " + total + "점 입니다."); // 총점 계산
}
}
public class Subject {
// field
private String name;
private int scorePoint;
// getter and setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScorePoint() {
return scorePoint;
}
public void setScorePoint(int scorePoint) {
this.scorePoint = scorePoint;
}
}