Java 20221102

신래은·2022년 12월 15일

JAVA

목록 보기
12/22

DAY 13

Code

CollectionEx

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

public class CollectionEx {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(10);
        list.add(20);
        list.add(30);
        System.out.println(list);
        
        List<String> strList = new ArrayList<String>();
        strList.add("abcde");
        strList.add("fghij");
        strList.add("klmno");
        System.out.println(strList);

        // List는 클래스 타입, 레퍼런스 타입의 변수에 한해서만 만들 수 있다.
        // 주소값으로 정보가 추가가 된다. 
        // <>안에 들어가는 정보는 내가 List로 사용할 타입을 설정하는 것 
        // 여기서 Point타입은 다른 클래스에서 내가 지정한 타입이다. (커스텀한)
        List<Point> /*인터페이스 */ ptList = new ArrayList/*실객체*/<Point>(); 
        // new가 하나여서 데이터를 하나만 만든 case.
        ptList.add(new Point(10,20));
        ptList.add(new Point(20,30));
        ptList.add(new Point(30,40));
        
        ptList.get(0).printPoint();
        ptList.get(1).printPoint();
        ptList.get(2).printPoint();
    }
}

Ex-Student

public class Student {
    Integer student_num;
    String name;
    Integer grade;
    String major;
    int sel;

    public Student (){}
    public Student (Integer student_num, String name, Integer grade, int sel) {
        this.student_num = student_num;
        this.name = name;
        this.grade = grade;
        setMajor(sel);
    }

    void StudentInfo () {
        System.out.println("학번 : " + student_num + " / 이름 : " + name + " / 학년 : " + grade + " / 전공 : " + major);
    }
    
    void setMajor (int sel) {
        String major[] = {"전산공학", "정보공학", "물리학", "국어교육과", "경영학", "국어국문학"};
        if (sel == 0 ) {
            this.major = major[0];
        }
        else if (sel == 1 ){
            this.major = "정보공학";
        }
        else if (sel == 2) {
            this.major = "물리학";
        }
        else if (sel == 3) {
            this.major = "국어교육과";
        }
        else if (sel == 4) {
            this.major = "경영학";
        }
        else if (sel == 5) {
            this.major = "국어국문학";
        }
    }
}

MainEx-Students

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

public class StudentMain {
    public static void main(String[] args) {
        List<Student> sList = new ArrayList<Student>();
        sList.add(new Student(20220001, "남형남", 1, 0));
        sList.add(new Student(20220002, "조현숙", 3, 1));
        sList.add(new Student(20220003, "남희준", 2,2));
        sList.add(new Student(20220004, "윤상원", 2, 3));
        sList.add(new Student(20220005, "문용태", 3, 0));
        sList.add(new Student(20220006, "사공채은", 4, 1));
        sList.add(new Student(20220007, "정문옥", 1, 4));
        sList.add(new Student(20220008, "황상훈", 2, 5));
        sList.add(new Student(20220009, "탁희아", 2, 3));
        sList.add(new Student(20220010, "봉성훈", 4, 1));
        
        for (Student s : sList) {
            s.StudentInfo();
        }
        // 풀어쓰는 방식
        Integer grade1 = 0 ;
        Integer grade2 = 0 ;
        Integer grade3 = 0 ;
        Integer grade4 = 0 ;
        for (Student s : sList) {
            // System.out.println(s.grade);  //1~4
            if (s.grade == 1) { grade1 ++; }
            else if (s.grade == 2) { grade2 ++; }
            else if (s.grade == 3) { grade3 ++; }
            else { grade4 ++; }
        }
        System.out.print("1학년 : " + grade1 + "명 / ");
        System.out.print("2학년 : " + grade2 + "명 / ");
        System.out.print("3학년 : " + grade3 + "명 / ");
        System.out.println("4학년 : " + grade4 + "명");
       
        // 줄여쓰는 방식
        int[] grade_cnt = {0, 0, 0, 0};
        for (Student s : sList) {
            grade_cnt[s.grade-1]++;
        }
        for(int i=0; i<4; i++) {
            System.out.print((i+1) + "학년 : " + grade_cnt[i] + "명 ");
            if (i != 3) {
                System.out.print("/ ");
            }
        }
        int[] major_cnt = {0, 0, 0, 0, 0};
        for (Student s : sList) {
            major_cnt[s.sel]++;
        }
        for (int i=0; i<5; i++) {
            System.out.println( major_cnt[i] + "명");
        }
    }
}

AccountEx

예금주와 잔액을 가지고 있는 Account 클래스를 만들고
Account 클래스의 객체를 담는 List 를 만들어서
3개의 Account 객체를 저장하는 코드를 작성하세요

public class Account {
    String name;
    Integer balance;

    public Account () {}
    public Account (String name, Integer balance) {
        this.name = name; this.balance=balance;
    }
    void printAccount () {
        System.out.println("예금주 : " + name + " /  잔액 : " + balance);
    }
}

Main

Account 객체를 저장하는 코드

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

public class AccountMain {
    public static void main(String[] args) {
        List<Account> accountList = new ArrayList<Account>();
        accountList.add (new Account("owner1", 2000));
        accountList.add (new Account("owner2", 3000));
        accountList.add (new Account("owner3", 4000));
        accountList.get(0).printAccount();
        accountList.get(1).printAccount();
        accountList.get(2).printAccount();
        System.out.println("accountList의 크기 : " + accountList.size());
       
        for(int i=0; i<accountList.size(); i++) {
            accountList.get(i).printAccount();
        }
        
        for (Account a : accountList) {
            a.printAccount();
        }
        
        Account acc = accountList.remove(1);
        
        for(int i=0; i<accountList.size(); i++) {
            accountList.get(i).printAccount();
        }
        acc.printAccount();
        
        // 교체 : 리스트 상의 0번 위치의 데이터/객체를 새로 만들어서
        // 기존에 있던 데이터/객체를 꺼내고, 새 데이터 객체를 저장
        Account old = accountList.set(0, new Account("급여통장", 1000000000));
        for (Account a : accountList) {
            a.printAccount();
        }
        old.printAccount();
        
        // 밀어넣기, 데이터를 추가 할 위치까지 지정
        accountList.add(0,new Account("급여통장2", 1000000000));
        for (Account a : accountList) {
            a.printAccount();
        }
    }
}

0개의 댓글