Source code ArrayList java


Array List is a class present in java.util package and is a part of Java Collecation Framework. ArrayList is implemented using a dynamic array data structure to store objects, hence it can grow or shrink whenever you add or remove elements from it. Here is an example of the implementation of custom ArrayList in java with the basic functions of the ArrayList class.

Also see: How to iterate ArrayList

Here the user-defined ArrayList class performs add[], addAll[], get[], set[], remove[], and a few more functionalities as listed below.

Implementation of Custom ArrayList with the following functionalities.

  • add[Object o]
  • add[int index, Object o]
  • addAll[Collection c]
  • remove[Object o]
  • get[int index]
  • set[int index, Object o]
  • contains[Object o]
  • size[]
  • isEmpty[]
  • clear[]

Java Source Code For Custom ArrayList:

MyArrayList.java

package javaCodeKorner.collection.ArrayList.userDefinedArrayList; import java.util.Collection; public class MyArrayList { private int size=0; private int capacity=10; private Object[] arr; //Creates empty ArrayList with default capacity 10 public MyArrayList[] { arr = new Object[capacity]; } //Creates empty ArrayList with specified initial capacity public MyArrayList[int arg] { this.capacity=arg; arr = new Object[capacity]; } /**Inserts the specified element at the end of the list * @param arg element to be added * @return true after successful insertion */ public boolean add[Object arg] { if [size 0] System.arraycopy[arr, i+1, arr, i,numMoved]; arr[--size]=null; return true; } } } else for [int i = 0; i < size; i++] { if [arr[i].equals[o]] { int numMoved = size - i - 1; if [numMoved > 0] System.arraycopy[arr, i+1, arr, i,numMoved]; arr[--size]=null; return true; } } return false; } /**Returns the specified element * * @param index * @return element at the specified position */ public Object get[int index] { if [index>=this.size] { throw new IndexOutOfBoundsException[]; } return arr[index]; } /**Replaces the element at the specified position in this list * with the specified element. * @param index * @param element * @return Object previous Object */ public Object set[int index, Object obj] { Object oldElement = arr[index]; arr[index] = obj; return oldElement; } /**Returns true if list contains specified element * else returns false * @param o element whose presence in the list is to be checked */ public boolean contains[Object o] { for [int i = 0; i < size; i++] { if [arr[i].equals[o]] { return true; } } return false; } /**Returns the size of the list*/ public int size[] { return size; } /**Returns true if list contains no elements * @return true if list is empty */ public boolean isEmpty[] { if [size==0] { return true; } else return false; } /**Removes all the elements from the list*/ public void clear[] { arr = new Object[capacity]; size=0; } }

Below are test classes to add, remove, set, get, etc. from the MyArrayList:

Example for add[] method

AddElements.java
public class AddElements { public static void main[String[] args] { MyArrayList list = new MyArrayList[]; list.add["123"]; list.add[123.456]; list.add["Naveen"]; list.add["Ravi"]; //Adding element at the specific position list.add[4, "Rajesh"]; //Iterating list by for-loop for [int i = 0; i < list.size[]; i++] { System.out.println[list.get[i]]; } } } Output:
123 123.456 Naveen Ravi Rajesh

Example For remove[] method.

RemoveElements.java
public class RemoveElements { public static void main[String[] args] { MyArrayList list = new MyArrayList[]; list.add["Rakesh"]; list.add["Mahesh"]; list.add["Naveen"]; list.add["Ravi"]; //Adding element at the specific position list.add[4, "Rajesh"]; System.out.println["Before removing"]; System.out.println["size:"+list.size[]]; for [int i = 0; i < list.size[]; i++] { System.out.println[list.get[i]]; } //Removing specified element list.remove["Naveen"]; System.out.println["*****************"]; System.out.println["After removing"]; System.out.println["size:"+list.size[]]; for [int i = 0; i < list.size[]; i++] { System.out.println[list.get[i]]; } } }
Output:
Before removing size:5 Rakesh Mahesh Naveen Ravi Rajesh ***************** After removing size:4 Rakesh Mahesh Ravi Rajesh

Example for get[] and set[] methods.

GetAndSetDemo.java
public class GetAndSetDemo { public static void main[String[] args] { MyArrayList list = new MyArrayList[]; list.add["Rakesh"]; list.add["Mahesh"]; list.add["Naveen"]; list.add["Ravi"]; System.out.println["Before update: "+list.get[1]]; //Replacing an element at a specified index list.set[1, "Kumar"]; System.out.println["After update: "+list.get[1]]; } } Output:
Before update: Mahesh After update: Kumar

Example for contains[], isEmpty[], size[] methods.

MyArrayListDemo.java
public class MyArrayListDemo { public static void main[String[] args] { MyArrayList list = new MyArrayList[]; list.add["Rakesh"]; list.add["Mahesh"]; list.add["Naveen"]; list.add["Ravi"]; System.out.println["contains: "+list.contains["Mahesh"]]; System.out.println["isEmpty: "+list.isEmpty[]]; System.out.println["size, before clearing: "+list.size[]]; list.clear[];//clearing array System.out.println["size, after clearing: "+list.size[]]; } } Output:
contains: true isEmpty: false size, before clearing: 4 size, after clearing: 0

See Also:

  • How to sort ArrayList in java

Like this post? let me know through the comment section below. And please give it a share.

Video liên quan

Chủ Đề