更新時間:2022-08-09 11:02:41 來源:動力節(jié)點 瀏覽3063次
Java創(chuàng)建對象數(shù)組的方法是什么?動力節(jié)點小編來告訴大家。Java 編程語言是關(guān)于類和對象的,因為它是一種面向?qū)ο蟮木幊陶Z言。當(dāng)我們需要在程序中存儲單個對象時,我們使用 Object 類型的變量來完成。但是當(dāng)我們處理大量對象時,最好使用對象數(shù)組。
Java對象數(shù)組這個名字本身就暗示它存儲了一個對象數(shù)組。與傳統(tǒng)的數(shù)組存儲值(如字符串、整數(shù)、布爾值等)不同,對象數(shù)組存儲對象,這意味著對象被存儲為數(shù)組的元素。請注意,當(dāng)我們說對象數(shù)組時,存儲在數(shù)組中的不是對象本身,而是對象的引用。
使用Object 類創(chuàng)建一個對象數(shù)組,我們知道 Object 類是所有類的根類。
我們使用Class_Name后跟一個方括號[],然后是對象引用名稱來創(chuàng)建一個對象數(shù)組。
Class_Name[ ] objectArrayReference;
或者,我們也可以將對象數(shù)組聲明為:
Class_Name objectArrayReference[ ];
上述兩個聲明都暗示objectArrayReference是一個對象數(shù)組。
例如,如果您有一個 Student 類,那么我們可以創(chuàng)建一個 Student 對象數(shù)組,如下所示:
學(xué)生[]學(xué)生對象;
或者
學(xué)生 studentObjects[];
句法:
Class_Name obj[ ]= new Class_Name[Array_Length];
例如,如果您有一個 Student 類,并且我們想要聲明和實例化一個具有兩個對象/對象引用的 Student 對象數(shù)組,那么它將被寫為:
學(xué)生[]學(xué)生對象=新學(xué)生[2];
并且一旦像這樣實例化對象數(shù)組,則需要使用 new 關(guān)鍵字創(chuàng)建對象數(shù)組的各個元素。
下圖顯示了對象數(shù)組的結(jié)構(gòu):
一旦對象數(shù)組被實例化,我們需要用值初始化它。我們不能以使用原始類型初始化的方式初始化數(shù)組,因為它與原始類型數(shù)組不同。在對象數(shù)組中,我們必須初始化數(shù)組的每個元素,即需要初始化每個對象/對象引用。
初始化對象數(shù)組的不同方法:
通過使用構(gòu)造函數(shù)
通過使用單獨的成員方法
1.通過使用構(gòu)造函數(shù):
在創(chuàng)建實際對象時,我們可以通過分別將值傳遞給構(gòu)造函數(shù)來為每個對象分配初始值。單獨的實際對象是使用其不同的值創(chuàng)建的。
下面的程序顯示了如何使用構(gòu)造函數(shù)初始化對象數(shù)組。
// 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();
}
}
輸出
學(xué)生 arr 0 中的學(xué)生數(shù)據(jù):
學(xué)生 ID 是:1701289270 學(xué)生姓名是:Satyabrata
學(xué)生 arr 1 中的學(xué)生數(shù)據(jù):
學(xué)生 ID 是:1701289219 學(xué)生姓名是:Omm Prasad
2.通過使用單獨的成員方法:
通過使用單獨的成員方法,我們也可以初始化對象。創(chuàng)建相應(yīng)類的成員函數(shù),用于將初始值分配給對象。
下面的程序顯示了如何使用單獨的成員方法初始化對象數(shù)組。
// 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 clas 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();
}
}
輸出
學(xué)生 arr 0 中的學(xué)生數(shù)據(jù):
學(xué)生 ID 是:1701289270 學(xué)生姓名是:Satyabrata
學(xué)生 arr 1 中的學(xué)生數(shù)據(jù):
學(xué)生 ID 是:1701289219 學(xué)生姓名是:Omm Prasad
讓我們看另一個使用初始值聲明對象數(shù)組的示例:
這里對象數(shù)組的聲明是通過添加初始值來完成的。
// 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]);
}
}
輸出
馬魯?shù)?
2019
鈴木
2019
通過上述介紹,相信大家對Java創(chuàng)建對象數(shù)組的方法已經(jīng)有所了解,大家如果對此比較感興趣,想了解更多相關(guān)知識,可以關(guān)注一下動力節(jié)點的Java基礎(chǔ)教程,里面有更豐富的知識等著大家去學(xué)習(xí),希望對大家能夠有所幫助。
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743