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程序,我们讨论了如何访问数据作为对象数组。

更新于:2023年5月2日

浏览量 102

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.