Chapter17. 클래스배열(객체배열)

YeongSeok·2022년 11월 12일
0

Java

목록 보기
23/25

객체 역시 배열로 다루는 것이 가능하며, 이를 객체 배열이라고 한다.

쉬운 예로는 학생을 관리하는 프로그램을 들 수 있다.

학생이라는 재사용클래스(이름,나이,주소)를 정의해준다

Student.java

//재사용 클래스
public class Student {

	//인스턴스변수 ==> 데이터 저장
	private String name;
	private int age;
	private String address;
	
	//생성자 ==> 인스턴스 초기화
	public Student() {
		
	}

	public Student(String name, int age, String address) {
		this.name = name;
		this.age = age;
		this.address = address;
	}

	//getter 메서드, setter 메서드
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
}

학생 관리 프로그램 객체

TestStudent.java

public class TestStudent {

public static void main(String[] args) {
	
	//1.개별적인 변수로 관리
	Student s1 = new Student("홍길동1",20,"서울1");
	Student s2= new Student("홍길동2",20,"서울2");
	Student s3 = new Student("홍길동3",20,"서울3");
	Student s4 = new Student("홍길동4",20,"서울4");
	Student s5 = new Student("홍길동5",20,"서울5");
	
	//2.배열로 관리(변수 하나로 관리 되어 효율적이다.)
	Student [] stuArr = new Student[5];
	stuArr[0] = s1;
	stuArr[1] = s2;
	stuArr[2] = s3;
	stuArr[3] = s4;
	stuArr[4] = s5;
	
	//가.new 이용
	stuArr[0] = new Student("홍길동1",20,"서울1");
	stuArr[1] = new Student("홍길동2",20,"서울2");
	stuArr[2] = new Student("홍길동3",20,"서울3");
	stuArr[3] = new Student("홍길동4",20,"서울4");
	stuArr[4] = new Student("홍길동5",20,"서울5");
	
	
	for(Student s : stuArr) {
		System.out.println(s.getName());
	}
	
	//2. 값
	Student [] stuArr2 = {new Student("홍길동1",20,"서울1"),new Student("홍길동2",20,"서울2"),new Student("홍길동3",20,"서울3")
						  };
	
	for(Student s : stuArr2) {
		System.out.println(">>>"+s.getName());
	}
	
	//3.new + 값
	
	Student [] stuArr3 = {new Student("홍길동1",20,"서울1"),
							new Student("홍길동2",20,"서울2"),
								new Student("홍길동3",20,"서울3")
	  						};
	
	
	 for(Student s : stuArr3) {
		 System.out.println("###"+s.getName());
	 }

}//end main

}

첫번째는 객체 배열을 개별적인 변수로 관리가 가능하다.

		Student s1 = new Student("홍길동1",20,"서울1");
		Student s2= new Student("홍길동2",20,"서울2");
		Student s3 = new Student("홍길동3",20,"서울3");
		Student s4 = new Student("홍길동4",20,"서울4");
		Student s5 = new Student("홍길동5",20,"서울5");**

두번째는 배열로 관리(하나의 변수로 관리하여 효율적이다.)

		Student [] stuArr = new Student[5];
		stuArr[0] = s1;
		stuArr[1] = s2;
		stuArr[2] = s3;
		stuArr[3] = s4;
		stuArr[4] = s5;

객체 생성 방법에는 3가지가 있습니다.

첫째는 new 키워드 사용하는 것입니다.

stuArr[0] = new Student("홍길동1",20,"서울1");
		stuArr[1] = new Student("홍길동2",20,"서울2");
		stuArr[2] = new Student("홍길동3",20,"서울3");
		stuArr[3] = new Student("홍길동4",20,"서울4");
		stuArr[4] = new Student("홍길동5",20,"서울5");

둘째는 리터럴(값)을 이용하여 배열을 생성 하는 것입니다.

Student [] stuArr2 = {new Student("홍길동1",20,"서울1"),new Student("홍길동2",20,"서울2"),new Student("홍길동3",20,"서울3")
							  };
		
		for(Student s : stuArr2) {
			System.out.println(">>>"+s.getName());
		}

마지막으로는 new 키워드와 리터얼(값) 같이 이용하여 배열을 생성이 가능합니다.

Student [] stuArr3 = {new Student("홍길동1",20,"서울1"),
								new Student("홍길동2",20,"서울2"),
									new Student("홍길동3",20,"서울3")
		  						};
		
		
		 for(Student s : stuArr3) {
			 System.out.println("###"+s.getName());
		 }
profile
끊임없이 성장하고 싶은 개발자

0개의 댓글