Java ArrayList Class - How to Create an ArrayList and Add Remove Clear Array Elements - APPFICIAL

Опубликовано: 07 Октябрь 2024
на канале: Appficial
16,889
146

Working with arrays can be difficult because they have a fixed size, and it’s not so easy to add or remove items. Java provides a class called ArrayList that provides many useful methods for dealing with a collection of data. An ArrayList is an abstract data type (ADT) implemented as a generic class, and supports different data. types. ArrayLists are declared as follows, where T is the ArrayLists’s type.

Here are some commonly used ArrayList methods:

add (T element) - Adds a new element to the ArrayList
remove (T element) Removes the first occurrence of the element
get (int index) - Return the element at the specified index
set (int index, T element) - Replace the element at the specified index with the new element
size() - Returns the number of elements in the ArrayList
isEmpty() - Returns true if the arrayList size is 0, false otherwise
clear() - Removes all elements from the ArrayList


SUBSCRIBE!