Array and Array Lists
Arrays
- Fixed length
- Anything => Primitives (integer, long, boolean) and also Objects (string, Dog, etc)
ArrayList
- Dynamic length
Only hold objects. ArrayList => Error! but can you can use its class wrapper, e.g.: Integer for int.
Access elements
array[1];
arrayList.get(1);
Get length
array.length;
arrayList.size();
Add elements
Cannot do it with Arrays
arrayList.add("New element"));
Set an element
array[0] = "New element";
arrayList.set(0, "New element");
Remove elements
Cannot be done it in arrays
arrayList.remove(1)
or
arrayList.remove("Name of element to be removed")
Array Lists and Linked Lists
- They are both part of the Collection framework and they implement the List interface.
LinkedList
- Each element is called a node and it has a reference / pointer to the next and the previous node.
- The linked list start with a pointer to the first element (HEAD).
- They are not in contiguous memory spots.
ArrayList
- Good performance for random access.
- Not so good when adding or removing new elements, because it needs to run over all the array for changing only one element.
Top comments (0)