📜  ArrayList 如何在Java中保存对象?

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

ArrayList 如何在Java中保存对象?

ArrayList 是集合框架的一部分,存在于Java.util 包中。它为我们提供了Java中的动态数组,就像 C++ 中的 Vector 一样。虽然,它可能比标准数组慢,但在需要对数组进行大量操作的程序中很有帮助。

为了理解问题,分为两部分:

  1. 前半部分:表示 ArrayList 中普通元素的通用添加。要存储在ArrayList 中的元素的数据类型是String 或Integer 类型。
  2. 后半部分:使用类对象作为元素制作相同的 ArrayList。

语法:声明列表 

ArrayList names = new ArrayList();

插图:考虑的子类随机命名为“人”。

方法: Java 中 List 接口的List.get()方法Java获取此列表中给定特定索引处的元素。

  • 制作一个辅助Java类。
  • 给它一些属性。例如,Person 类可以包括姓名、年龄、号码等
  • 创建一个新对象。
  • 将上面创建的对象存储在 ArrayList 中。
  • 打印使用List.get()方法创建的上述对象的元素。

执行:

  • 制作一个 Person 类对象。
  • 使用 Person 对象作为元素制作一个 ArrayList。

(A) 前半部分:表示 ArrayList 中普通元素的泛型加法。

例子

Java
// Java Program to add elements to List
 
// Importing ArrayList class from
// java.util package
import java.util.ArrayList;
 
// Class
public class GFG {
 
    // Mai driver method
    public static void main(String[] args)
    {
        // Creating an ArrayList object
        // (Declaring List of String type)
        ArrayList names = new ArrayList();
 
        // Adding (appending) elements to List
        // Custom inputs using add() method
        names.add("Computer");
        names.add("Science");
        names.add("Portal");
 
        // Printing all the elements of ArrayList
        // Declaring generic ArrayList of String type
        System.out.print(names);
    }
}


Java
// Java Program to show
// How objects can an ArrayList hold
 
// Importing ArrayList class from
// java.util package
import java.util.ArrayList;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Make Person data-type objects
        Person p1 = new Person("Aditya", 19);
        Person p2 = new Person("Shivam", 19);
        Person p3 = new Person("Anuj", 15);
 
        // Create an ArrayList object
        //(Declaring List of Person type)
        ArrayList names = new ArrayList();
 
        // Adding objects to the ArrayList
        names.add(p1);
        names.add(p2);
        names.add(p3);
 
        // Print and display the elements of adobe ArrayList
        // using get() method
        System.out.println(names.get(0).name);
        System.out.println(names.get(0).age);
        System.out.println(names.get(1).name);
        System.out.println(names.get(1).age);
        System.out.println(names.get(2).name);
        System.out.println(names.get(2).age);
 
        // New Line
        System.out.println();
 
        // Optional Part for better understanding
        System.out.println(
            "Optional Part Added For Better Understanding");
 
        // (Optional)
        // Displaying what happens if printed by simply
        // passing List object as parameter
        System.out.println(names);
    }
}
 
// Class user-defined
class Person {
 
    // Random properties associated with the person
    // Person name
    String name;
    // Person age
    int age;
 
    // Constructor for class Person
    // for initializing objects
    Person(String name, int age)
    {
        // This keyword for efering to current object
        this.name = name;
        this.age = age;
    }
}


输出
[Computer, Science, Portal]

B) 后半部分:使用类对象作为元素制作相同的 ArrayList。

例子

Java

// Java Program to show
// How objects can an ArrayList hold
 
// Importing ArrayList class from
// java.util package
import java.util.ArrayList;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Make Person data-type objects
        Person p1 = new Person("Aditya", 19);
        Person p2 = new Person("Shivam", 19);
        Person p3 = new Person("Anuj", 15);
 
        // Create an ArrayList object
        //(Declaring List of Person type)
        ArrayList names = new ArrayList();
 
        // Adding objects to the ArrayList
        names.add(p1);
        names.add(p2);
        names.add(p3);
 
        // Print and display the elements of adobe ArrayList
        // using get() method
        System.out.println(names.get(0).name);
        System.out.println(names.get(0).age);
        System.out.println(names.get(1).name);
        System.out.println(names.get(1).age);
        System.out.println(names.get(2).name);
        System.out.println(names.get(2).age);
 
        // New Line
        System.out.println();
 
        // Optional Part for better understanding
        System.out.println(
            "Optional Part Added For Better Understanding");
 
        // (Optional)
        // Displaying what happens if printed by simply
        // passing List object as parameter
        System.out.println(names);
    }
}
 
// Class user-defined
class Person {
 
    // Random properties associated with the person
    // Person name
    String name;
    // Person age
    int age;
 
    // Constructor for class Person
    // for initializing objects
    Person(String name, int age)
    {
        // This keyword for efering to current object
        this.name = name;
        this.age = age;
    }
}
输出
Aditya
19
Shivam
19
Anuj
15

Optional Part Added For Better Understanding
[Person@214c265e, Person@448139f0, Person@7cca494b]