ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java just as Vector in C++. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.
In order to understand the problem is divided into two halves:
- First Half: Representing generic addition of a normal element in the ArrayList. Data-type of the elements that are to be stored in the ArrayList is either String or Integer type.
- Second half: Making the same ArrayList with class objects as elements.
Syntax: Declaring ListÂ
ArrayList<String> names = new ArrayList<String>();
Here <String> represents the data-type of the elements that is to be stored in the ArrayList. Remember that this can not be any primitive data-type such as int or float.
Illustration: Sub Class taken for consideration is randomly named ‘Person’.
Method: List.get() method of List interface in Java is used to get the element present in this list at a given specific index.
- Make an auxiliary java class.
- Give it some properties. For example, class Person can include name, age, number, etc
- Create a new object.
- Store the above-created object inside the ArrayList.
- Print elements of above object created using List.get() method.
Implementation:Â
- Making a Person class object.
- Making an ArrayList with Person objects as elements.
(A) First Half: Representing generic addition of a normal element in the ArrayList.
Example
Java
// Java Program to add elements to ListÂ
// Importing ArrayList class from// java.util packageimport java.util.ArrayList;Â
// Classpublic class GFG {Â
    // Mai driver method    public static void main(String[] args)    {        // Creating an ArrayList object        // (Declaring List of String type)        ArrayList<String> names = new ArrayList<String>();Â
        // 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);    }} |
[Computer, Science, Portal]
B) Second half: Making the same ArrayList with class objects as elements.
ExampleÂ
Java
// Java Program to show// How objects can an ArrayList holdÂ
// Importing ArrayList class from// java.util packageimport java.util.ArrayList;Â
// Classpublic 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<Person> names = new ArrayList<Person>();Â
        // 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-definedclass 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]
Â
Â
Â
