更新時間:2022-08-09 11:02:41 來源:動力節點 瀏覽3098次
Java創建對象數組的方法是什么?動力節點小編來告訴大家。Java 編程語言是關于類和對象的,因為它是一種面向對象的編程語言。當我們需要在程序中存儲單個對象時,我們使用 Object 類型的變量來完成。但是當我們處理大量對象時,最好使用對象數組。
Java對象數組這個名字本身就暗示它存儲了一個對象數組。與傳統的數組存儲值(如字符串、整數、布爾值等)不同,對象數組存儲對象,這意味著對象被存儲為數組的元素。請注意,當我們說對象數組時,存儲在數組中的不是對象本身,而是對象的引用。
使用Object 類創建一個對象數組,我們知道 Object 類是所有類的根類。
我們使用Class_Name后跟一個方括號[],然后是對象引用名稱來創建一個對象數組。
Class_Name[ ] objectArrayReference;
或者,我們也可以將對象數組聲明為:
Class_Name objectArrayReference[ ];
上述兩個聲明都暗示objectArrayReference是一個對象數組。
例如,如果您有一個 Student 類,那么我們可以創建一個 Student 對象數組,如下所示:
學生[]學生對象;
或者
學生 studentObjects[];
句法:
Class_Name obj[ ]= new Class_Name[Array_Length];
例如,如果您有一個 Student 類,并且我們想要聲明和實例化一個具有兩個對象/對象引用的 Student 對象數組,那么它將被寫為:
學生[]學生對象=新學生[2];
并且一旦像這樣實例化對象數組,則需要使用 new 關鍵字創建對象數組的各個元素。
下圖顯示了對象數組的結構:
一旦對象數組被實例化,我們需要用值初始化它。我們不能以使用原始類型初始化的方式初始化數組,因為它與原始類型數組不同。在對象數組中,我們必須初始化數組的每個元素,即需要初始化每個對象/對象引用。
初始化對象數組的不同方法:
通過使用構造函數
通過使用單獨的成員方法
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();
}
}
輸出
學生 arr 0 中的學生數據:
學生 ID 是:1701289270 學生姓名是:Satyabrata
學生 arr 1 中的學生數據:
學生 ID 是:1701289219 學生姓名是: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 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();
}
}
輸出
學生 arr 0 中的學生數據:
學生 ID 是:1701289270 學生姓名是:Satyabrata
學生 arr 1 中的學生數據:
學生 ID 是:1701289219 學生姓名是: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]);
}
}
輸出
馬魯蒂
2019
鈴木
2019
通過上述介紹,相信大家對Java創建對象數組的方法已經有所了解,大家如果對此比較感興趣,想了解更多相關知識,可以關注一下動力節點的Java基礎教程,里面有更豐富的知識等著大家去學習,希望對大家能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習