๋ฐ์ํ
โถ ArrayList ํด๋์ค
๊ธฐ์กด ๋ฐฐ์ด์ ๋จ์ ์ ๋ณด์ํจ.
ex) ArrayList<Book> library = new ArrayList<Book>( );
package array;
import java.util.ArrayList; // ArrayList ํด๋์ค import
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<Book> library = new ArrayList<Book>(); // ArrayList ์ ์ธ
library.add(new Book("ํ๋ฐฑ์ฐ๋งฅ", "์กฐ์ ๋"));
library.add(new Book("๋ฐ๋ฏธ์", "ํค๋ฅด๋ง ํค์ธ"));
library.add(new Book("์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ", "์ ์๋ฏผ"));
library.add(new Book("ํ ์ง", "๋ฐ๊ฒฝ๋ฆฌ"));
library.add(new Book("์ด๋ฆฐ์์", "์ํ
์ฅํ๋ฆฌ"));
for (int i = 0; i < library.size(); i++) { // ๋ฐฐ์ด์ ์ถ๊ฐ๋ ์์ ๊ฐ์๋งํผ ์ถ๋ ฅ
Book book = library.get(i);
book.showBookInfo();
}
System.out.println();
System.out.println("=== ํฅ์๋ for๋ฌธ ์ฌ์ฉ ===");
for (Book book : library) {
book.showBookInfo();
}
}
}
-get ์ผ๋ก ๋ค์ด์ค๋๊ฑด 'ํ' ์ผ๋ก ์๊ฐํ๋ฉด ํธํจ.
-๋ด์ฉ์ด '์ด' ์ด ๋๋ค๊ณ ์๊ฐํ๋ฉด ํธํจ.
์ค์ต ) ArrayList ํ์ฉํ๊ธฐ
211์ชฝ์ <๋ ํผ์ ์ฝ๋ฉ>์์ ๋ง๋ Student ํด๋์ค๋ฅผ ์ด์ฉํ์ฌ StudentArrayList ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ArrayList<Student> ์๋ฃํ์ ArrayList๋ฅผ ์ ์ธํ๋ค. ArrayList์ ํ์ 3๋ช ์ ์ถ๊ฐํ๊ณ ๊ทธ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ผ. ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ๋ค.
<๋ด๊ฐ ํ ๊ฒ>
-์๋ฃํ๊ณผ ๋ฐฐ์ด ์ด๋ฆ ์ ํ ๋ ์ฃผ์ํ ๊ฒ!
โถ ๋ฐฐ์ด ์์ฉ ํ๋ก๊ทธ๋จ
์ง๊ธ๊น์ง ๋ฐฐ์ด ArrayList๋ฅผ ์ฌ์ฉํด ํ์ ์ฑ์ ์ถ๋ ฅ ํ๋ก๊ทธ๋จ์ ๊ตฌํํด๋ณด์.
package arraylist;
import java.util.ArrayList;
public class Student {
int studentID;
String studentName;
ArrayList<Subject> subjectList; // ArrayList ์ ์ธํ๊ธฐ
public Student(int studentID, String studentName) { // ์์ฑ์
this.studentID = studentID;
this.studentName = studentName;
subjectList = new ArrayList<Subject>(); // ArrayList ์์ฑํ๊ธฐ
}
public void addSubject(String name, int score) { // ํ์์ด ์๊ฐํ๋ ๊ณผ๋ชฉ์ SubjectList ๋ฐฐ์ด์ ํ๋์ฉ ์ถ๊ฐํ๋ ๋ฉ์๋
Subject subject = new Subject(); // Subject ์์ฑํ๊ธฐ
subject.setName(name); // ๊ณผ๋ชฉ ์ด๋ฆ ์ถ๊ฐํ๊ธฐ
subject.setScorePoint(score); // ์ ์ ์ถ๊ฐํ๊ธฐ
subjectList.add(subject); // ๋ฐฐ์ด์ ์ ์ฅํ๊ธฐ
}
public void showStudentInfo() {
int total = 0;
for (Subject s : subjectList) { // ๋ฐฐ์ด ์์ ๊ฐ ์ถ๋ ฅ
total += s.getScorePoint(); // ์ด์ ๋ํ๊ธฐ
System.out.println("ํ์ " + studentName + "์ " + s.getName() + " ๊ณผ๋ชฉ ์ฑ์ ์ " + s.getScorePoint() + "์
๋๋ค.");
}
System.out.println("ํ์ " + studentName + "์ ์ด์ ์ " + total + " ์
๋๋ค.");
}
}
package arraylist;
public class Subject {
private String name; // Subject ํด๋์ค์ ๋ฉค๋ฒ ๋ณ์
private int scorePoint; // Subject ํด๋์ค์ ๋ฉค๋ฒ ๋ณ์
public class Subject() {
}
public Subject(String name, int scorePoint) {
this.name = name;
this.scorePoint = scorePoint;
}
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;
}
}
package arraylist;
public class StudentTest {
public static void main(String[] args) {
Student studentLee = new Student(1001, "Lee");
studentLee.addSubject("๊ตญ์ด", 100);
studentLee.addSubject("์ํ", 50);
Student studentKim = new Student(1002, "Kim");
studentKim.addSubject("๊ตญ์ด", 70);
studentKim.addSubject("์ํ", 85);
studentKim.addSubject("์์ด", 100);
studentLee.showStudentInfo();
System.out.println("======================================");
studentKim.showStudentInfo();
}
}
<์คํ ๊ฒฐ๊ณผ>
โ
๋ฐ์ํ
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ์์ (0) | 2021.02.19 |
---|---|
[Java] ์ฐ์ต๋ฌธ์ (0) | 2021.02.19 |
[Java] ์ด์ฐจ์ ๋ฐฐ์ด (0) | 2021.02.19 |
[Java] ํฅ์๋ for๋ฌธ๊ณผ ๋ฐฐ์ด (0) | 2021.02.19 |
[Java] ๋ฐฐ์ด ๋ณต์ฌํ๊ธฐ (0) | 2021.02.19 |
๋๊ธ