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:
- Create an object of the ArrayList class and place its data type as the class data.
- Define a class and create a constructor in that class, and put the required entities in it.
- Link those entities to the global variables.
- 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.