티스토리 뷰

Algorithm/Java

9장 퀴즈

JJIINDOL 2023. 7. 3. 15:29

문제> 

ArrayList를 이용하여 학생별 프로그래밍 언어 자격증 취득 정보를 관리하고, 자바 자격증을 보유한 학생 이름을 출력하시오.

 

조건>

- 학생 이름 및 자격증 취득 정보를 위한 Student 클래스 생성

- 학생 1인당 보유 자격증은 1개로 제한

- 모든 클래스는 하나의 파일에 정의

이름 자격증(프로그래밍 언어)
유재석 파이썬
박명수 자바
김종국 자바
조세호 C
서장훈 파이썬

 

 

<정답 코드>

import java.util.ArrayList;
import java.util.List;

class Student {
    String name;
    String certification;

    public Student(String name, String certification) {
        this.name = name;
        this.certification = certification;
    }
}

public class _Quiz_09 {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();

        list.add(new Student("유재석", "파이썬"));
        list.add(new Student("박명수", "자바"));
        list.add(new Student("김종국", "자바"));
        list.add(new Student("조세호", "C"));
        list.add(new Student("서장훈", "파이썬"));

        System.out.println("자바 자격증을 보유한 학생");
        System.out.println("---------------------");

        for (Student student : list) {
            if (student.certification.equals("자바")) {
                System.out.println(student.name);
            }
        }
    }
}

 

<정리>

1. '==' 연산자와 equals() 메소드의 차이점

• == 연산자는 변수가 동일한 객체를 참조하는지를 확인하는 데 사용된다. 반면 equals() 메소드는 객체의 내용을 비교하는 데 사용된다. 

String str1 = "Hello";
String str2 = "Hello";

System.out.println(str1 == str2);  // true

 

String str1 = new String("Hello");
String str2 = new String("Hello");

System.out.println(str1.equals(str2));  // true

 

2. ArrayList<> 에서 <> 안에 들어갈 수 있는 타입은 자바에서 사용 가능한 모든 참조 타입이다. 즉, 클래스 타입, 인터페이스 타입 등을 지정할 수 있다.

ArrayList<String> stringList = new ArrayList<>();       // 문자열을 저장하는 ArrayList
ArrayList<Integer> integerList = new ArrayList<>();     // 정수를 저장하는 ArrayList
ArrayList<Double> doubleList = new ArrayList<>();       // 실수를 저장하는 ArrayList
ArrayList<MyClass> objectList = new ArrayList<>();      // 사용자 정의 클래스 객체를 저장하는 ArrayList
ArrayList<List<String>> nestedList = new ArrayList<>(); // 문자열 리스트를 저장하는 중첩 ArrayList

 

'Algorithm > Java' 카테고리의 다른 글

자바 기본편 - 7장 퀴즈  (0) 2023.03.04
자바 기본편 - 상속  (0) 2023.02.21
자바 기본편 - 접근 제어자  (0) 2023.02.20
자바 기본편- Getter 와 Setter  (0) 2023.02.18
자바 기본편 - 용어 정리  (0) 2023.02.13
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/09   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함