๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java] ArrayList ํด๋ž˜์Šค

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 19.
๋ฐ˜์‘ํ˜•

โ–ถ 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

๋Œ“๊ธ€