Package com.collection.arraylist
Class CustomArrayList<E>
java.lang.Object
com.collection.arraylist.CustomArrayList<E>
- Type Parameters:
E- the type of elements stored in the list
- All Implemented Interfaces:
CustomList<E>
This class represents a custom implementation of a dynamic array list data structure.
It implements the CustomList interface and provides methods to add, remove, get elements, and more.
The list automatically expands in capacity when needed to accommodate additional elements.
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an instance of CustomArrayList with the default capacity.CustomArrayList(int capacity) Constructs an instance of CustomArrayList with the specified capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoidMethod to insert the specified element at the specified index in the list.booleanMethod to add the specified element to the end of the list.booleanMethod to add all the elements in the specified array to the end of the list.voidclear()Removes all of the elements from this list.booleanMethod to check if the list contains the specified element.voidensureCapacity(int minCapacity) Method to increase the capacity of this CustomArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.get(int index) Method to get the element at the specified index in the list.intMethod which returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.remove(int index) Method to remove the element at the specified index from the list.booleanMethod to remove the first occurrence of the specified element from the list.Method to replace the element at the specified index with the specified element.intsize()Returns the number of elements in the list.toString()Method which returns a string representation of this list.
-
Constructor Details
-
CustomArrayList
public CustomArrayList()Constructs an instance of CustomArrayList with the default capacity. -
CustomArrayList
public CustomArrayList(int capacity) Constructs an instance of CustomArrayList with the specified capacity.- Parameters:
capacity- the initial capacity of the list
-
-
Method Details
-
size
public int size()Returns the number of elements in the list.- Specified by:
sizein interfaceCustomList<E>- Returns:
- size - the number of elements in the list
-
get
Method to get the element at the specified index in the list.- Specified by:
getin interfaceCustomList<E>- Parameters:
index- the index of the element to retrieve- Returns:
- the element at the specified index
-
add
Method to add the specified element to the end of the list.- Specified by:
addin interfaceCustomList<E>- Parameters:
element- the element to be added- Returns:
- true if the element was successfully added, false otherwise
-
add
Method to insert the specified element at the specified index in the list.- Specified by:
addin interfaceCustomList<E>- Parameters:
index- the index at which to insert the elementelement- the element to insert in the list
-
addAll
Method to add all the elements in the specified array to the end of the list.- Specified by:
addAllin interfaceCustomList<E>- Parameters:
elements- an array of elements to be added- Returns:
- true if all elements were successfully added, false otherwise
-
remove
Method to remove the element at the specified index from the list.- Specified by:
removein interfaceCustomList<E>- Parameters:
index- the index of the element to remove- Returns:
- the removed element
-
remove
Method to remove the first occurrence of the specified element from the list.- Specified by:
removein interfaceCustomList<E>- Parameters:
element- the element to be removed- Returns:
- true if the element was found and removed, false otherwise
-
contains
Method to check if the list contains the specified element.- Specified by:
containsin interfaceCustomList<E>- Parameters:
element- the element to check for- Returns:
- true if the element is found in the list, false otherwise
-
set
Method to replace the element at the specified index with the specified element.- Specified by:
setin interfaceCustomList<E>- Parameters:
index- the index of the element to replaceelement- the new element to set- Returns:
- the old element at the specified index
-
indexOf
Method which returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.- Specified by:
indexOfin interfaceCustomList<E>- Parameters:
elem- the element to search for- Returns:
- the index of the specified element in this list, or -1 if not found
-
clear
public void clear()Removes all of the elements from this list. The list will be empty after this call.- Specified by:
clearin interfaceCustomList<E>
-
toString
Method which returns a string representation of this list. The string representation consists of a list of the elements enclosed in square brackets ("[]"), separated by commas and spaces. -
ensureCapacity
public void ensureCapacity(int minCapacity) Method to increase the capacity of this CustomArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.- Parameters:
minCapacity- the desired minimum capacity of this CustomArrayList.
-