logo

Java에서 객체 배열을 만드는 방법은 무엇입니까?

전제 조건 – Java에서 객체를 생성하는 다양한 방법

Java 프로그래밍 언어의 모든 것 클래스와 객체 그것은 객체지향 프로그래밍 언어 . 프로그램에 저장할 단일 객체가 필요한 경우 Object 유형의 변수를 사용하여 수행합니다. 그러나 수많은 객체를 다룰 때는 객체 배열을 사용하는 것이 더 좋습니다.



객체 배열이라는 이름 자체는 객체 배열을 저장한다는 의미입니다. 기존 배열과 달리 문자열, 정수, 부울 등과 같은 값을 저장합니다. 객체 배열 백화점 사물 이는 객체가 배열의 요소로 저장된다는 의미입니다. 우리가 말할 때 객체 배열 배열에 저장되는 것은 객체 자체가 아니라 객체의 참조입니다.

Java에서 객체 배열 생성 –

객체 배열은 다음을 사용하여 생성됩니다. 객체 클래스 , 그리고 우리는 Object 클래스가 모든 클래스의 루트 클래스라는 것을 알고 있습니다.



우리는 클래스_이름 그 뒤에 대괄호가 옵니다. [] 그런 다음 객체 참조 이름을 사용하여 객체 배열을 만듭니다.

Class_Name[ ] objectArrayReference;>

또는 다음과 같이 객체 배열을 선언할 수도 있습니다.

Class_Name objectArrayReference[ ];>

위의 두 선언 모두 다음을 의미합니다. 객체 배열 참조 객체의 배열입니다.



예를 들어, Student 클래스가 있는 경우 아래와 같이 Student 개체 배열을 만들 수 있습니다.

문자열 하위 문자열
Student[ ] studentObjects; Or Student studentObjects[];>

객체 배열 인스턴스화 –

통사론:

Class_Name obj[ ]= new Class_Name[Array_Length];>

예를 들어, Student 클래스가 있고 두 개의 객체/객체 참조가 있는 Student 객체의 배열을 선언하고 인스턴스화하려는 경우 다음과 같이 작성됩니다.

Student[ ] studentObjects = new Student[2];>

객체 배열이 이와 같이 인스턴스화되면 new 키워드를 사용하여 객체 배열의 개별 요소를 생성해야 합니다.

아래 그림은 객체 배열의 구조를 보여줍니다.

객체 배열 초기화

객체 배열이 인스턴스화되면 값으로 초기화해야 합니다. 기본 유형의 배열과 다르기 때문에 기본 유형으로 초기화하는 방식으로 배열을 초기화할 수 없습니다. 객체 배열에서는 배열의 각 요소를 초기화해야 합니다. 즉, 각 객체/객체 참조를 초기화해야 합니다.

객체 배열을 초기화하는 다양한 방법:

  1. 생성자를 사용하여
  2. 별도의 회원방법을 이용하여

1. 생성자를 사용하여:

실제 객체를 생성할 때 객체에 값을 전달하여 각 객체에 초기값을 할당할 수 있습니다. 건설자 갈라져. 개별 실제 개체는 고유한 값으로 생성됩니다.

C# 튜토리얼

아래 프로그램은 생성자를 사용하여 객체 배열을 초기화하는 방법을 보여줍니다.

자바




// Java program to demonstrate initializing> // an array of objects using constructor> class> GFG {> >public> static> void> main(String args[])> >{> >// Declaring an array of student> >Student[] arr;> >// Allocating memory for 2 objects> >// of type student> >arr =>new> Student[>2>];> >// Initializing the first element> >// of the array> >arr[>0>] =>new> Student(>1701289270>,>'Satyabrata'>);> >// Initializing the second element> >// of the array> >arr[>1>] =>new> Student(>1701289219>,>'Omm Prasad'>);> >// Displaying the student data> >System.out.println(> >'Student data in student arr 0: '>);> >arr[>0>].display();> >System.out.println(> >'Student data in student arr 1: '>);> >arr[>1>].display();> >}> }> // Creating a student class with> // id and name as a attributes> class> Student {> >public> int> id;> >public> String name;> >// Student class constructor> >Student(>int> id, String name)> >{> >this>.id = id;> >this>.name = name;> >}> >// display() method to display> >// the student data> >public> void> display()> >{> >System.out.println(>'Student id is: '> + id +>' '> >+>'and Student name is: '> >+ name);> >System.out.println();> >}> }>

>

for 루프의 종류

>

산출

Student data in student arr 0: Student id is: 1701289270 and Student name is: Satyabrata Student data in student arr 1: Student id is: 1701289219 and Student name is: Omm Prasad>

2. 별도의 회원 방식을 사용하여 :

별도의 멤버 메소드를 사용하여 객체를 초기화할 수도 있습니다. ㅏ 멤버 함수 각 클래스가 생성되어 객체에 초기 값을 할당하는 데 사용됩니다.

아래 프로그램은 별도의 멤버 메서드를 사용하여 개체 배열을 초기화하는 방법을 보여줍니다.

자바




xdxd가 무슨 뜻이야?

// Java program to demonstrate initializing> // an array of objects using a method> class> GFG {> >public> static> void> main(String args[])> >{> >// Declaring an array of student> >Student[] arr;> >// Allocating memory for 2 objects> >// of type student> >arr =>new> Student[>2>];> >// Creating actual student objects> >arr[>0>] =>new> Student();> >arr[>1>] =>new> Student();> >// Assigning data to student objects> >arr[>0>].setData(>1701289270>,>'Satyabrata'>);> >arr[>1>].setData(>1701289219>,>'Omm Prasad'>);> >// Displaying the student data> >System.out.println(> >'Student data in student arr 0: '>);> >arr[>0>].display();> >System.out.println(> >'Student data in student arr 1: '>);> >arr[>1>].display();> >}> }> // Creating a Student class with> // id and name as a attributes> class> Student {> >public> int> id;> >public> String name;> >// Method to set the data to> >// student objects> >public> void> setData(>int> id, String name)> >{> >this>.id = id;> >this>.name = name;> >}> >// display() method to display> >// the student data> >public> void> display()> >{> >System.out.println(>'Student id is: '> + id +>' '> >+>'and Student name is: '> >+ name);> >System.out.println();> >}> }>

>

>

산출

Student data in student arr 0: Student id is: 1701289270 and Student name is: Satyabrata Student data in student arr 1: Student id is: 1701289219 and Student name is: Omm Prasad>

객체 배열이 초기 값으로 선언되는 또 다른 예를 살펴보겠습니다.

여기 객체 배열 선언은 초기 값을 추가하여 수행됩니다.

자바




정렬된 배열 목록 자바

// Java program to demonstrate an array> // of objects is declared with initial values.> class> GFG {> >public> static> void> main(String args[])> >{> >// Creating an array of objects> >// declared with initial values> >Object[] javaObjectArray> >= {>'Maruti'>,>new> Integer(>2019>),>'Suzuki'>,> >new> Integer(>2019>) };> >// Printing the values> >System.out.println(javaObjectArray[>0>]);> >System.out.println(javaObjectArray[>1>]);> >System.out.println(javaObjectArray[>2>]);> >System.out.println(javaObjectArray[>3>]);> >}> }>

>

>

산출

Maruti 2019 Suzuki 2019>