Created: 2023-08-27 17:11
Status: #concept
Subject: Programming
Tags: Java java.util Linked List Java Arrays java.util.Collection
java.util.ArrayList
It is a
java.util.*
class that is a dynamically-sized array which we can import in order to use the new ArrayList<T>(int size | Collection<T> arr)
.
- we have to declare it with a
<T>
Java Generic Type, similar to TypeScript Generics. - to use the
<T>
generic, theT
must be the Java Data Type Class and not the primitive type, soint != Integer
. - because of this, the values in an
ArrayList
need to be of the same data type.
// import the list so the program can use it
import java.util.ArrayList;
public class Program {
public static void main(String[] args) {
// create a list
ArrayList<String> wordList = new ArrayList<>();
wordList.add("First"); // add to the last index, 0
wordList.add("Second"); // add to the last index, 1
System.out.println(wordList.get(1)); // "Second"
}
}
They are useful because they only store Java Class object instance Reference Types instead of Value Types.
- Reference Types are stored in the Memory Heap while the Value Type primitives are in the Execution Stack.
Common Methods
.size()
returns the length of the list..get(int index)
returns the element atindex
..set(int index, T elem)
sets theindex
element toelem
..add(T elem)
addselem
to the end of the list..add(int index, T elem)
addselem
to a specificindex
.
.remove(int index)
removes the element at the givenindex
.remove(Object elem)
removeselem
from the list, we need to typecast withInteger.valueOf(int)
because a primitiveint
will remove anindex
.
.indexOf(T elem)
returns the index ofelem
..contains(Object elem)
returnstrue
ifelem
is in the list, otherwisefalse
..clone()
returns a shallow copy of the instance..sort(Comparator<? super E> c)
sorts the list according to the java.util.Comparator functionc
.- use
Collections.sort(arrayList)
to mutate and sort an ArrayList based on its natural order and.compareTo
implementation.
- use
Consumer Methods
A Consumer is a function that takes a single value and returns
void
.
- we can use a Java Anonymous Function
(...args) -> { ... }
.
.forEach((val) -> { ... })
iterates through each element likefor (T elem : list) { ... }
.
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create an ArrayList which going to
// contains a list of Numbers
ArrayList<Integer> Numbers = new ArrayList<Integer>();
// Add Number to list
Numbers.add(23);
Numbers.add(32);
Numbers.add(45);
Numbers.add(63);
// forEach method of ArrayList and
// print numbers
Numbers.forEach((n) -> System.out.println(n));
}
}
Automatic Type Generic Inference
When we are instantiating a
new ArrayList<T>()
, we can omit T
if the variable that is being assigned the new ArrayList<>()
is already declared.
When we do ArrayList<String> names = new ArrayList<>();
, the assigned list will be of type String
.
public class AmusementParkRide {
private String name;
private int minimumHeigth;
private int visitors;
private ArrayList<Person> riding;
public AmusementParkRide(String name, int minimumHeigth) {
this.name = name;
this.minimumHeigth = minimumHeigth;
this.visitors = 0;
this.riding = new ArrayList<>();
}
// ..
}