
1. ๋ฌธ์
Student ํด๋์ค๋ฅผ ์ ์ํ๊ณ , LinkedList๋ฅผ ์ฌ์ฉํ์ฌ "์ ๊ธฐ์ ์" ์ ๊ณต ํ์๋ง ํํฐ๋งํ๋ solution ๋ฉ์๋๋ฅผ ์์ฑํด์ผ ํ๋ ๋ฌธ์
import java.util.LinkedList;
public class Assignment03 {
static class Student {
String name;
String major;
public Student(String name, String major) {
this.name = name;
this.major = major;
}
public String getName() {
return name;
}
public String getMajor() {
return major;
}
@Override
public String toString() {
return "์ด๋ฆ: " + name + ", ์ ๊ณต: " + major;
}
}
public static void main(String[] args) {
LinkedList<Student> students = new LinkedList<>();
// ํ์ ์ถ๊ฐ
students.add(new Student("๋ฌธ00", "์ ๊ธฐ์ ์"));
students.add(new Student("์ 00", "์ํํธ์จ์ด"));
students.add(new Student("์ต00", "์ ๊ธฐ์ ์"));
students.add(new Student("๊น00", "์ฐ์
๋์์ธ"));
students.add(new Student("์00", "์ ๊ธฐ์ ์"));
System.out.println("์ ๊ณต์ด ์ ๊ธฐ์ ์์ธ ํ์ ๋ชฉ๋ก : ");
solution(students);
}
public static void solution(LinkedList<Student> students) {
// ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฉด์ ์ ๊ณต์ด "์ ๊ธฐ์ ์"์ธ ํ์์ ์ฐพ์ ์ถ๋ ฅ
for (Student student : students) {
if (student.getMajor().equals("์ ๊ธฐ์ ์")) {
System.out.println(student);
}
}
}
}
๋ฌธ์ ๊ฐ์ ๋ถ์ :
1. Student ํด๋์ค :
name๊ณผ major๋ผ๋ ๋๊ฐ์ ํ๋๋ฅผ ๊ฐ์ง ํด๋์ค์ด๋ค.
์์ฑ์์ getter ๋ฉ์๋, toString ๋ฉ์๋๊ฐ ์ ์๋์ด ์๋ค.
์ด ํด๋์ค๋ ํ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์ฌ์ฉ๋๋ค.
2. LinkedList :
์๋ฐ์์ ์ ๊ณตํ๋ List์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด ์ค ํ๋์ด๋ค.
ํ์ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ ์๋ฃ๊ตฌ์กฐ๋ก ์ฌ์ฉ๋๋ค.
LinkedList :
๋ ธ๋ ๊ธฐ๋ฐ์ ์๋ฃ๊ตฌ์กฐ๋ก, ๊ฐ ๋ ธ๋๊ฐ ๋ฐ์ดํฐ์ ๋ค์ ๋ ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ๋ฅผ ๊ฐ์ง๋ค.
์ฝ์ , ์ญ์ ๊ฐ ๋น๋ฒํ ๊ฒฝ์ฐ์ ์ ์ฉํ๋ฉฐ, ์์ฐจ์ ์ผ๋ก ํ์ํด์ผ ํ ๊ฒฝ์ฐ ์ฑ๋ฅ์ด ๋ฎ์์ง ์ ์๋ค.
3. solution ๋ฉ์๋ :
LinkedList์ ์ ์ฅ๋ ํ์ ๊ฐ์ฒด๋ฅผ ์ํํ๋ฉฐ, major๊ฐ "์ ๊ธฐ์ ์"์ธ ํ์๋ง ํํฐ๋ง ํ์ฌ ์ถ๋ ฅํ๋ค.
๊ฐ์ฒด ํํฐ๋ง์ด๋?
์ปฌ๋ ์ ์ ์ํํ๋ฉฐ, ๊ฐ ๊ฐ์ฒด์ ํน์ ์กฐ๊ฑด์ ๊ฒ์ฌํ์ฌ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ํํฐ๋งํ ์ ์๋ค.
์กฐ๊ฑด์ ๊ฒ์ฌํ ๋ ๊ฐ์ฒด์ ํ๋๋ฅผ ์ฐธ์กฐํ๊ธฐ ์ํด getter ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
๋ฌธ์ ํ์ด
1. Student ํด๋์ค
static class Student {
String name;
String major;
public Student(String name, String major) {
this.name = name;
this.major = major;
}
public String getName() {
return name;
}
public String getMajor() {
return major;
}
@Override
public String toString() {
return "์ด๋ฆ : " + name + ", ์ ๊ณต: " + major;
}
}
2. solution ๋ฉ์๋
๋งค๊ฐ๋ณ์๋ก ๋ฐ์ LinkedList๋ฅผ ์ํํ๋ค.
๊ฐ ํ์์ major ํ๋๋ฅผ ํ์ธํ๊ณ , "์ ๊ธฐ์ ์"์ ์ผ์นํ๋ ๊ฒฝ์ฐ ์ถ๋ ฅํ๋ค.
์ํ๋ฐฉ๋ฒ์ for- each ๋ฃจํ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฆฌ์คํธ์ ๊ฐ ์์์ ์ ๊ทผํ๋ค.
public static void solution(LinkedList<Student> students) {
// ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฉฐ ์กฐ๊ฑด์ ๋ง๋ ํ์ ์ถ๋ ฅ
for (Student student : students) {
if (student.getMajor().equals("์ ๊ธฐ์ ์")) {
System.out.println(student);
}
}
}
3. main ๋ฉ์๋
ํ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ LinkedList์ ์ถ๊ฐํ๋ค.
solution๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ํํฐ๋ง๋ ํ์ ๋ชฉ๋ก์ ์ถ๋ ฅํ๋ค.
public static void main(String[] args) {
LinkedList<Student> students = new LinkedList<>();
// ํ์ ์ถ๊ฐ
students.add(new Student("๋ฌธ00", "์ ๊ธฐ์ ์"));
students.add(new Student("์ 00", "์ํํธ์จ์ด"));
students.add(new Student("์ต00", "์ ๊ธฐ์ ์"));
students.add(new Student("๊น00", "์ฐ์
๋์์ธ"));
students.add(new Student("์00", "์ ๊ธฐ์ ์"));
System.out.println("์ ๊ณต์ด ์ ๊ธฐ์ ์์ธ ํ์ ๋ชฉ๋ก : ");
solution(students);
}
< ์ง๋ ๋์ ๊ฒ์๋ฌผ >
[Java] ArrayList vs LinkedList (์ถ๊ฐ/์ญ์ ๋ Linked, ์กฐํ๋ Array)
1. ArrayList์ LinkedList์ ์ฐจ์ดArrayList์ LinkedList๋ Java์์ ์ ๊ณตํ๋ ๋ ๊ฐ์ง ์ฃผ์ List ๊ตฌํ์ฒด๋ก, ๊ฐ๊ฐ์ ๊ตฌ์กฐ์ ์ฌ์ฉ ๋ชฉ์ ์ด ๋ค๋ฅด๋ค. ArrayList- ๊ตฌ์กฐ : ๋ด๋ถ์ ์ผ๋ก ๋์ ๋ฐฐ์ด(Dynamic Array)์ ์ฌ์ฉํ๋ค.-
yeonbikim.tistory.com