배열과 클래스의 관계

강9·2023년 12월 4일
0

Java

목록 보기
51/71
post-thumbnail

🔖 동일한 구조와 이질적인 구조

배열(Array)클래스(Class)는 데이터를 담는 역할은 같지만 만들어지는 구조는 서로 다르다.

배열(Array)은 동일한 데이터를 담는 동일한 구조이며,
클래스(Class)는 서로 다른 데이터를 담는 이질적인 구조이다.


int[] arr = new int[4];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;

// 객체생성 -> Student std = new Student();


public class Student {
// 멤버변수, 상태변수
	public String name;
    public String dept;
    public int age;
    public String email;
    public int tear;
    public String phone;
}
// 객체배열 -> Student[] std = new Student[4];

public class StudentTest {
    public static void main(String[] args) {
 		
	Student[] std = new Student[4];
    	std[0] = new Student("김김김", "컴퓨터공학과",20, "abc@naver.com", 20231110, "010-1234-5678");
        std[1] = new Student("이이이", "컴퓨터공학과",21, "abcd@naver.com", 20231111, "010-1234-5679");
        std[2] = new Student("박박박", "컴퓨터공학과",22, "abcde@naver.com", 20231113, "010-1234-5670");
        std[3] = new Student("오오오", "컴퓨터공학과",23, "abcdef@naver.com", 20231118, "010-1234-5671");
        
    }
}

객체배열(Object Array) : 객체를 저장하고 있는 배열

profile
코린이 일기

0개의 댓글