- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Custom ArrayList in Java
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
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("
Employee 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
Advertisements