Custom ArrayList in Java

Prerequisite – eks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeek's main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about

ArrayList in Java
ArrayList in Java (equivalent to vector in C++) having a dynamic size. It can be shrunk or expanded based on size. ArrayList is a part of the collection framework and is present in java.util package.
An ArrayList:

ArrayList <E> list = new ArrayList <> ();

E here represents an object datatype e.g. Integer. The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
More about Integer Object: here
list
Custom ArrayList: A custom ArrayList has attributes based on user requirements and can have more than one type of data. This data is provided by a custom inner class that is formed by a combination of various primitive object datatypes.

Consider a case when we have to take input as N number of students and details are:
roll number, name, marks, phone number
A normal method using object ArrayList would be:


 

   // define 4 ArrayLists and save data accordingly in
   // each of them.
    
   // roll number arraylist
   ArrayList<Integer> roll = new ArrayList<>(); 

   // name arraylist
   ArrayList<String> name = new ArrayList<>();

   // marks arraylist
   ArrayList<Integer> marks = new ArrayList<>();

   // phone number arraylist
   ArrayList<Long> phone = new ArrayList<>();

   // and for n students
   for(int i = 0; i < n; i++)
   {
     // add all the values to each arraylist 
     // each arraylist has primitive datatypes
     roll.add(rollnum_i);
     name.add(name_i);
     marks.add(marks_i);
     phone.add(phone_i);
  }

Now using a custom Arraylist:
The custom ArrayList simply supports multiple data in the way as shown in this image.
custom
To construct Custom ArrayList

  • Build an ArrayList Object and place its type as a Class Data.
  • Define a class and put the required entities in the constructor.
  • Link those entities to global variables.
  • Data received from the ArrayList is of that class type which stores multiple data.

filter_none

edit

play_arrow

brightness_4

// Java program to illustrate customArraylist in Java

import java.util.ArrayList;

  

class CustomArrayList

{

    // custom class which has data type

    // class has defined the type of data ArrayList

    // size of input 4

    int n=4;

  

    // the custom datatype class

    class Data

    {

        // global variables of the class

        int roll;

        String name;

        int marks;

        long phone;

  

        // constructor has type of data that is required

        Data(int roll, String name, int marks, long phone)

        {

            // initialize the input variable from main

            // function to the global variable of the class

            this.roll = roll;

            this.name = name;

            this.marks = marks;

            this.phone = phone;

        }

    }

  

    public static void main(String args[])

    {

        // suppose the custom input data

        int roll[] = {1, 2, 3, 4};

        String name[] = {"Shubham", "Atul", "Ayush", "Rupesh"};

        int marks[] = {100, 99, 93, 94};

        long phone[] = {8762357381L, 8762357382L, 8762357383L,

                        8762357384L

                       };

  

        // Create an object of the class

        CustomArrayList custom = new CustomArrayList();

  

        // and call the function to add the values to the arraylist

        custom.addValues(roll, name, marks, phone);

    }

  

    public void addValues(int roll[], String name[], int marks[],

                          long phone[])

    {

        // local custom arraylist of data type

        // Data having (int, String, int, long) type

        // from the class

        ArrayList<Data> list=new ArrayList<>();

  

        for (int i = 0; i < n; i++)

        {

            // create an object and send values to the

            // constructor to be saved in the Data class

            list.add(new Data(roll[i], name[i], marks[i],

                                              phone[i]));

        }

  

        // after adding values printing the values to test

        // the custom arraylist

        printValues(list);

    }

  

    public void printValues(ArrayList<Data> list)

    {

        // list- the custom arraylist is sent from

        // previous function

  

        for (int i = 0; i < n; i++)

        {

            // the data received from arraylist is of Data type

            // which is custom (int, String, int, long)

            // based on class Data

  

            Data data = list.get(i);

  

            // data variable of type Data has four primitive datatypes

            // roll -int

            // name- String

            // marks- int

            // phone- long

            System.out.println(data.roll+" "+data.name+" "

                               +data.marks+" "+data.phone);

        }

    }

}

Output:

1 Shubham 100 8762357381
2 Atul 99 8762357382
3 Ayush 93 8762357383
4 Rupesh 94 8762357384


References:

  • Oracle documentation
  • ArrayLists

 

Related Articles

post a comment