Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Implementation
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Custom ArrayList

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Most of the programming languages which are currently being used support the use of an array. Arrays are generally defined as linear data structures that store homogenous elements in a continuous manner in the memory. An array is a basic functionality provided by Java, whereas an ArrayList is a class of Java collection frameworks that belongs to the Java.util package. An ArrayList is dynamic in size that is, it can shrink or expand based on the size. The problem or the limitation of using the ArrayList is that it only allows the user to store data of the same type (heterogeneous elements). To overcome this problem, we make and use a Custom ArrayList.

Also read, Duck Number in Java and Hashcode Method in Java

Implementation

The syntax which is followed in order to declare an ArrayList in Java is :
 

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


In the above syntax, type represent an object datatype.

 

 

The above diagram shows how elements of the same type are stored in an ArrayList. 

 


The above diagram shows how a custom ArrayList which is formed by the combination of various primitive object datatypes, will look like in Java.

Suppose we want to record the details such as name, phone number, email, the salary of all the employees working in an office then, using the traditional ArrayList, we need to have 4 different ArrayList, one for each of the attributes, which would look like: 
 

ArrayList<String> name = new ArrayList<>(); // name
ArrayList<Long> phone = new ArrayList<>();  // phone number
ArrayList<String> email = new ArrayList<>();  // email
ArrayList<Long> salary = new ArrayList<>(); //  salary


In order to fetch the data of each employee we need to run a loop like :
 

for (int i = 0; i < n; i++) 
{
  name.add(name_i);
  phone.add(phone_i);
  email.add(email_i);
  salary.add(salary_i);
}


If we were to do the same thing, using a custom array list, we would follow the following steps:

  1. Create an object of the ArrayList class and place its data type as the class data.
  2. Define a class and create a constructor in that class, and put the required entities in it.
  3. Link those entities to the global variables.
  4. Data received from the ArrayList is of the class that stores multiple data.


Code

// Importing ArrayList class
import java.util.ArrayList;

// CustomArrayList
class Example {

    // Custom class
    // size of input 4
    int n = 4;

    // The custom datatype class
    class Data {

        // Global variables of the class
        long salary;
        String name;
        String email;
        long phone;

        // Constructor has type of data that is required
        Data(long salary, String name, String email, long phone)
        {
            // this keyword refers to current instance
            this.salary = salary;
            this.name = name;
            this.email = email;
            this.phone = phone;
        }
    }

    // Method 1- Driver method
    public static void main(String args[])
    {
          // Custom input data
        String name[] = { "John", "Captain", "Thor", "Roger" };
        long phone[] = { 8269540012L, 8269540012L,
                        8269540012L, 8269540012L };
        String email[] = { "john@xyz.com", "captain@xyz.com", "thor@xyz.com", "roger@xyz.com" };
        long salary[] = { 100000, 90000,
                        190000, 80000 };

        // Creating an object of the class
        Example object = new Example();
        object.addValues(salary, name, email, phone);
    }

    public void addValues(long salary[], String name[], String email[], long phone[])
    {
        // Data having (String, String, long, long) type from the class
        ArrayList<Data> list = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            // create an object & send values to constructor
            list.add(new Data(salary[i], name[i], email[i],
                              phone[i]));
        }
        // print
        Display(list);
    }

    // Method 2-To display
    public void Display(ArrayList<Data> list)
    {
        for (int i = 0; i < n; i++) {
            Data data = list.get(i);
            System.out.println("Name: "+data.name + ", Phone:" + data.phone
                              + ", Email:" + data.email + ", Salary:"
                              + data.salary);
        }
    }
}

 

Must Read Difference between ArrayList and LinkedList and  Swap Function in Java


Output

Name: John, Phone:8269540012, Email:john@xyz.com, Salary:100000
Name: Captain, Phone:8269540012, Email:captain@xyz.com, Salary:90000
Name: Thor, Phone:8269540012, Email:thor@xyz.com, Salary:190000
Name: Roger, Phone:8269540012, Email:roger@xyz.com, Salary:80000

Practice by yourself on online java compiler.

You can also checkout Java List Iterator here.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

  1. What is an array?
    An array is a collection of homogeneous elements stored at contiguous memory locations.
     
  2. What is a custom ArrayList?
    A custom ArrayList is an array whose attributes are based on user requirements and can have more than one data type.
     
  3. How can we implement a custom ArrayList?
    We can implement a custom ArrayList in Java using classes.

Key Takeaways

In this article, we have extensively discussed what a custom ArrayList is and how we can implement it in Java programming language with examples and their implementation in Visual Studio Code.

Recommended problems -

 

We hope that this blog has helped you enhance your knowledge regarding custom ArrayList in the Java programming language and if you would like to learn more, check out our articles on understanding association, aggregation, and composition in Java, understanding overloading and overriding static methods, what is hierarchical inheritance in Java.

Do upvote our blog to help other ninjas grow. Happy Coding!”

Previous article
CopyOnWriteArrayList
Next article
Java ListIterator method
Live masterclass