Custom ArrayList in Java

Java 8Object Oriented ProgrammingProgramming

A custom ArrayList can have multiple types of data and its attributes in general are based on the user requirements.

A program that demonstrates a custom ArrayList is given as follows −

Example

 Live Demo

import java.util.ArrayList;
public class CustomArrayList {
   int n = 5;
   class Employee {
      int eno;
      String name;
      Employee(int eno, String name) {
         this.eno = eno;
         this.name = name;
      }
   }
   public static void main(String args[]) {
      int eno[] = {101, 102, 103, 104, 105};
      String name[] = {"Jane", "Mary", "Adam", "Harry", "John"};
      CustomArrayList caList = new CustomArrayList();
      caList.add(eno, name);
   }
   public void add(int eno[], String name[]) {
      ArrayList<Employee> aList = new ArrayList<>();
      for (int i = 0; i < n; i++) {
         aList.add(new Employee(eno[i], name[i]));
      }
      print(aList);
   }
   public void print(ArrayList<Employee> aList) {
      for (int i = 0; i < n; i++) {
         Employee e = aList.get(i);
         System.out.println("\nEmployee Number: " + e.eno);
         System.out.println("Employee Name: " + e.name);
      }
   }
}

The output of the above program is as follows −

Output

Employee Number: 101
Employee Name: Jane

Employee Number: 102
Employee Name: Mary

Employee Number: 103
Employee Name: Adam

Employee Number: 104
Employee Name: Harry

Employee Number: 105
Employee Name: John
raja
Published on 28-Feb-2019 07:22:12
Advertisements