Javaでオブジェクトの配列を作成するには?

Javaでオブジェクトの配列を作成するには?

前提条件 – Java でオブジェクトを作成するさまざまな方法

Java プログラミング言語のすべて クラスとオブジェクト それは オブジェクト指向プログラミング言語 。プログラムに単一のオブジェクトを保存する必要がある場合は、Object 型の変数を使用してそれを行います。ただし、多数のオブジェクトを扱う場合は、オブジェクトの配列を使用することをお勧めします。

オブジェクトの配列 名前自体が、オブジェクトの配列を格納することを示唆しています。従来の配列とは異なり、文字列、整数、ブール値などの値を格納します。 オブジェクトの配列 店舗 オブジェクト つまり、オブジェクトは配列の要素として格納されます。と言うときは注意してください。 オブジェクトの配列 配列に格納されるのはオブジェクト自体ではなく、オブジェクトの参照です。

Java でオブジェクトの配列を作成する –

オブジェクトの配列は、 オブジェクトクラス 、オブジェクト クラスがすべてのクラスのルート クラスであることがわかります。

私たちが使用するのは、 クラス名 角括弧が後に続く [] 次にオブジェクト参照名を入力してオブジェクトの配列を作成します。

Class_Name[ ] objectArrayReference; 

あるいは、オブジェクトの配列を次のように宣言することもできます。

Class_Name objectArrayReference[ ]; 

上記の宣言は両方とも、次のことを意味します。 objectArrayReference オブジェクトの配列です。

たとえば、クラス Student がある場合、以下に示す Student オブジェクトの配列を作成できます。

Student[ ] studentObjects; Or Student studentObjects[]; 

オブジェクトの配列をインスタンス化します –

構文:

Class_Name obj[ ]= new Class_Name[Array_Length]; 

たとえば、Student クラスがあり、2 つのオブジェクト/オブジェクト参照を持つ Student オブジェクトの配列を宣言してインスタンス化したい場合、次のように記述されます。

Student[ ] studentObjects = new Student[2]; 

このようにオブジェクトの配列がインスタンス化されたら、new キーワードを使用してオブジェクトの配列の個々の要素を作成する必要があります。

以下の図は、オブジェクトの配列の構造を示しています。

オブジェクトの配列を初期化しています

オブジェクトの配列がインスタンス化されたら、値を使用して初期化する必要があります。配列はプリミティブ型の配列とは異なるため、プリミティブ型で初期化する方法では初期化できません。オブジェクトの配列では、配列の各要素を初期化する必要があります。つまり、各オブジェクト/オブジェクト参照を初期化する必要があります。

オブジェクトの配列を初期化するさまざまな方法:

  1. コンストラクターを使用することで
  2. 別メンバー方式を使用する場合

1. コンストラクターを使用する場合:

実際のオブジェクトの作成時に、オブジェクトに値を渡すことで、各オブジェクトに初期値を割り当てることができます。 コンストラクタ 別々に。個々の実際のオブジェクトは、それぞれ異なる値で作成されます。

以下のプログラムは、コンストラクターを使用してオブジェクトの配列がどのように初期化されるかを示しています。

ジャワ




// 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();> > }> }>

出力

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. 別のメンバーメソッドを使用する場合:

別のメンバー メソッドを使用することによって、オブジェクトを初期化することもできます。あ メンバー関数 それぞれのクラスの が作成され、オブジェクトに初期値を割り当てるために使用されます。

以下のプログラムは、別のメンバー メソッドを使用してオブジェクトの配列がどのように初期化されるかを示しています。

ジャワ




// 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