Java程式訪問所有資料作為物件陣列
陣列是一種線性資料結構,用於儲存具有相似資料型別的一組元素。我們可以使用基本資料型別建立陣列,並且由於類被認為是使用者定義的資料型別,因此也可以建立物件陣列。
在本文中,我們將討論物件陣列,並將建立一個Java程式來訪問所有資料作為物件陣列。
物件陣列或物件的陣列
物件的陣列實際上包含物件的引用變數,即物件陣列中儲存的元素是引用型別。我們遵循與建立基本型別陣列和物件陣列相同的語法。但是,在物件陣列的情況下,我們使用類名而不是基本資料型別。
基本型別陣列的語法
Data_Type[] nameOfarray;
// declaration
Or,
Data_Type nameOfarray[];
// declaration
Or,
// declaration with size
Data_Type nameOfarray[] = new Data_Type[sizeofarray];
// declaration and initialization
Data_Type nameOfarray[] = {values separated with comma};
示例
String[] item = new String[5];
在上面的例子中,我們建立了一個字串陣列,可以儲存5個字串元素。
物件陣列的語法
Class_name objectArray[]; // declaration Or, // declaration and instantiation Class_name objectArray[] = new Class_name[sizeofarray];
示例
Cart[ ] obj = new Cart[5];
在上面的例子中,我們建立了一個物件陣列,可以儲存5個Cart類的物件。我們使用了類名而不是基本資料型別。
記住,當我們宣告和初始化物件陣列時,它不會自動為元素建立物件,我們需要為每個元素單獨建立物件。
物件陣列例項化後,我們需要用值初始化陣列的元素。在這種情況下,元素是物件。一種傳遞值的方法是使用類的建構函式,或者我們可以建立多個物件,然後將它們傳遞給另一個物件陣列。
語法
arrayObject_name[index] = new constructor_name( values ); Or, arrayObject_name[index] = object_name;
我們將在下一節中看到示例。
Java程式訪問所有資料作為物件陣列
示例1
在下面的示例中,我們將建立一個物件陣列並使用建構函式用值初始化它。
class Cart {
String item;
double price;
Cart(String item, int price) {
// Constructor
this.item = item;
this.price = price;
}
}
public class Main {
public static void main(String[] args) {
Cart[ ] obj = new Cart[5];
// creation of object array
// Passing values to the array object
obj[0] = new Cart("Rice", 59);
obj[1] = new Cart("Milk", 60);
obj[2] = new Cart("Bread", 45);
obj[3] = new Cart("Peanut", 230);
obj[4] = new Cart("Butter", 55);
System.out.println("Accessing data as Object Array: ");
int i = 0;
// initialization of loop variable
while(i < obj.length) {
// to iterate through array obejct
System.out.println("Item: " +obj[i].item + ", " + "Price: " +obj[i].price);
// to print the values
i++;
// incrementing loop variable
}
}
}
輸出
Accessing data as Object Array: Item: Rice, Price: 59.0 Item: Milk, Price: 60.0 Item: Bread, Price: 45.0 Item: Peanut, Price: 230.0 Item: Butter, Price: 55.0
在上面的示例中,我們建立了類“Cart”及其建構函式“Cart”,它接受兩個引數“item”和“price”。在主方法中,我們建立了大小為5的“Cart”類物件陣列“obj”。使用建構函式“Cart”初始化陣列的元素。我們使用了while迴圈來列印值。
示例2
下面的示例說明了另一種訪問資料作為物件陣列的方法。
class Cart {
String item;
double price;
}
public class Arrayobj {
public static void main(String []args) {
// Initializing the values to the variables
Cart c1 = new Cart();
// object 1
c1.item = "Rice";
c1.price = 59;
Cart c2 = new Cart();
// object 2
c2.item = "Milk";
c2.price = 60;
Cart c3 = new Cart();
// object 3
c3.item = "Bread";
c3.price = 45;
Cart obj[] = new Cart[3];
// array of object
// Passing objects to object array
obj[0] = c1;
obj[1] = c2;
obj[2] = c3;
for(int i = 0; i < obj.length ; i++ ) {
System.out.println("Item: " +obj[i].item + ", " + "Price: " +obj[i].price);
}
}
}
輸出
Item: Rice, Price: 59.0 Item: Milk, Price: 60.0 Item: Bread, Price: 45.0
在上面的例子中,我們建立了類'Cart'和它的三個物件'c1','c2','c3'。我們還建立了一個大小為3的'Cart'類物件陣列'obj'。使用這些物件初始化陣列的元素。我們使用了for迴圈來列印值。
結論
在本文中,我們瞭解了基本型別陣列和物件陣列之間的異同。藉助兩個Java程式,我們討論瞭如何訪問資料作為物件陣列。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP