📜  如何在Java中创建对象数组?

📅  最后修改于: 2022-05-13 01:55:25.148000             🧑  作者: Mango

如何在Java中创建对象数组?

先决条件 - 在Java中创建对象的不同方法

Java编程语言是关于类和对象的,因为它是一种面向对象的编程语言。当我们需要在程序中存储单个对象时,我们使用 Object 类型的变量来完成。但是当我们处理大量对象时,最好使用对象数组。

对象数组这个名字本身就暗示它存储了一个对象数组。与传统的数组存储值(如字符串、整数、布尔值等)不同,对象数组存储对象,这意味着对象被存储为数组的元素。请注意,当我们说对象数组时,存储在数组中的不是对象本身,而是对象的引用。

在Java中创建对象数组 –

使用 Object 类创建一个对象数组,我们知道 Object 类是所有类的根类。

我们使用Class_Name后跟一个方括号[] ,然后是对象引用名称来创建一个对象数组。

Class_Name[ ] objectArrayReference;

或者,我们也可以将对象数组声明为:

Class_Name objectArrayReference[ ];

上述两个声明都暗示objectArrayReference是一个对象数组。

例如,如果您有一个 Student 类,那么我们可以创建一个 Student 对象数组,如下所示:

Student[ ] studentObjects;  
Or
Student studentObjects[];

实例化对象数组——

句法:

Class_Name obj[ ]= new Class_Name[Array_Length];

例如,如果您有一个 Student 类,并且我们想要声明和实例化一个具有两个对象/对象引用的 Student 对象数组,那么它将被写为:

Student[ ] studentObjects = new Student[2];

并且一旦像这样实例化对象数组,则需要使用 new 关键字创建对象数组的各个元素。

下图显示了对象数组的结构:

初始化对象数组

一旦对象数组被实例化,我们需要用值初始化它。我们不能以使用原始类型初始化的方式初始化数组,因为它与原始类型数组不同。在对象数组中,我们必须初始化数组的每个元素,即需要初始化每个对象/对象引用。

初始化对象数组的不同方法:

  1. 通过使用构造函数
  2. 通过使用单独的成员方法

1.通过使用构造函数:

在创建实际对象时,我们可以通过分别将值传递给构造函数来为每个对象分配初始值。单独的实际对象是使用其不同的值创建的。

下面的程序显示了如何使用构造函数初始化对象数组。

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


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


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


输出
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

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

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