Java ArrayList Video Tutorial

Post on 15-Jan-2017

496 views 1 download

transcript

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayListMarcus Biel, Software Craftsman

http://www.marcus-biel.com

2016, Marcus Biel, http://www.marcus-biel.com/

Collection Interface Hierarchy

<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implements

extends

ArrayList is the default implementation of the List interface.

2016, Marcus Biel, http://www.marcus-biel.com/

Collection Interface Hierarchy

<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implements

extends

As with any implementation of List, you can have duplicate elements in your ArrayList,

and you can go from element to element in the same order as the elements were inserted.

2016, Marcus Biel, http://www.marcus-biel.com/

Collection Interface Hierarchy

<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implements

extends

As it is based on arrays, ArrayList provides fast access, but inserting or removing an element

at a random position requires more time, as this will require to reorganize the list.

2016, Marcus Biel, http://www.marcus-biel.com/

Collection Interface Hierarchy

<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implements

extends

Fast access however is crucial for most applications, which is why ArrayList is the most commonly used Collection.

To store data that changes frequently, however, consider using an alternative container, for example LinkedList.

2016, Marcus Biel, http://www.marcus-biel.com/

SizeCapacity

Terminology

Before continuing, let me introduce you to two different terms which are important

to understand in context with ArrayList: Size and capacity.

2016, Marcus Biel, http://www.marcus-biel.com/

SizeCapacity

Terminology

Size is the number of elements the ArrayList currently holds. For every element you add to the list, the size will grow by one.

2016, Marcus Biel, http://www.marcus-biel.com/

SizeCapacity

Terminology

Capacity however is the number of elements the currently underlying Array can hold.

2016, Marcus Biel, http://www.marcus-biel.com/

TerminologySizeCapacity

The capacity of the ArrayList grows in intervals. The ArrayList starts with an initial capacity.

Every time you exceed the capacity of the Array, the ArrayList copies the data over to a new Array that is

about fifty percent larger than the previous one.

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayList Capacity…10

Let’s say you want to add one hundred elements to an ArrayList of an initial capacity of ten.

As the list grows, the system will create six more arrays to take the place of the first.

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayList Capacity…15

…10

First one array that can hold fifteen elements …

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayList Capacity

…22…15

…10

…then one for a maximum of twenty two elements…

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayList Capacity

…22…15

…10

…33

…then arrays with a capacity of thirty three…

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayList Capacity

…49

…22…15

…10

…33

…forty nine…

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayList Capacity

…73…49

…22…15

…10

…33

…seventy three…

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayList Capacity

…109…73

…49

…22…15

…10

…33

and finally one hundred and nine elements to hold the growing list.

These restructuring arrangements can negatively impact performance.

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayList CapacityList<String> myList = new ArrayList<>(INITIAL_CAPACITY);

You can instantly create an Array of the correct size to minimize these merging activities by

defining the correct capacity at creation time.

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayList CapacityList<String> myList = new ArrayList<>(INITIAL_CAPACITY);

In case you don’t know the final size of the ArrayList at creation time,

estimate it as close as possible. Choosing a too large capacity however can also

negatively impact performance,so choose this value carefully.

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayList CapacityList<String> myList = new ArrayList<>(INITIAL_CAPACITY);

I advise you to always explicitly set the capacity at creation time, as it documents your intentions.

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayList CapacityLow performance requirements are no

excuse for sloppy design and poor implementation.

For most projects you won’t have to worry about optimizing performance due to powerful hardware,

but this is no excuse for sloppy design and poor implementation!

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayListpackage java.util;

public class ArrayList<E> {

private static final int DEFAULT_CAPACITY = 10;

private Object[] elementData; private int size;

public E get(int index) {…} public boolean add(E e) {…}

public E remove(int index) {…}

[…]}

Here you can see a simplified extract of the class ArrayList. Keep in mind the real class looks a bit more complicated.

This is just meant to give you a more concrete idea of what the class ArrayList looks like.

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayListpackage java.util;

public class ArrayList<E> {

private static final int DEFAULT_CAPACITY = 10;

private Object[] elementData; private int size;

public E get(int index) {…} public boolean add(E e) {…}

public E remove(int index) {…}

[…]} As you can see,

ArrayList is just a class anyone could have written, given enough time and knowledge.

There is no black magic. You can find the actual source code online.

2016, Marcus Biel, http://www.marcus-biel.com/

ArrayListpackage java.util;

public class ArrayList<E> {

private static final int DEFAULT_CAPACITY = 10;

private Object[] elementData; private int size;

public E get(int index) {…} public boolean add(E e) {…}

public E remove(int index) {…}

[…]}

However, don’t rely too much on internals that you spot in the source code,

as they may change any time, if they are not defined in the Java language specification.

2016, Marcus Biel, http://www.marcus-biel.com/

private static final int DEFAULT_CAPACITY = 10;private Object[] elementData;private int size;

public E remove(int index) {…}public boolean add(E e) {…}public E get(int index) {…}

package java.util;public class ArrayList<E> {

[…]

}

ArrayList

default capacity is the initial size of the array, when you don’t specify it as I recommended before.

2016, Marcus Biel, http://www.marcus-biel.com/

private Object[] elementData;private int size;

public E remove(int index) {…}

private static final int DEFAULT_CAPACITY = 10;

public boolean add(E e) {…}public E get(int index) {…}

package java.util;public class ArrayList<E> {

[…]

}

ArrayList

“elementData” is the Array used to store the elements of the ArrayList.

2016, Marcus Biel, http://www.marcus-biel.com/

private Object[] elementData;private int size;

public E remove(int index) {…}

private static final int DEFAULT_CAPACITY = 10;

public boolean add(E e) {…}public E get(int index) {…}

package java.util;public class ArrayList<E> {

[…]

}

ArrayList

int size is the number of elements the ArrayList currently holds.

2016, Marcus Biel, http://www.marcus-biel.com/

private Object[] elementData;private int size;

public E remove(int index) {…}

private static final int DEFAULT_CAPACITY = 10;

public boolean add(E e) {…}public E get(int index) {…}

package java.util;public class ArrayList<E> {

[…]

}

ArrayList

…get…

2016, Marcus Biel, http://www.marcus-biel.com/

public boolean add(E e) {…}

private Object[] elementData;private int size;

public E remove(int index) {…}

private static final int DEFAULT_CAPACITY = 10;

public E get(int index) {…}

package java.util;public class ArrayList<E> {

[…]

}

ArrayList

…add..

2016, Marcus Biel, http://www.marcus-biel.com/

private Object[] elementData;private int size;

public E remove(int index) {…}

private static final int DEFAULT_CAPACITY = 10;

public boolean add(E e) {…}public E get(int index) {…}

package java.util;public class ArrayList<E> {

[…]

}

ArrayList

… and remove are some of the many functions ArrayList provides.

We will look at those methods now.

2016, Marcus Biel, http://www.marcus-biel.com/

Methods Overview<<interface>

>Collection

<<interface>>

List

ArrayList

implementsextends

So let me give you a short overview of the methods of the ArrayList class.

To make things easy for you, I have broken up the overview into methods belonging

to the java.util.Collection interface and methods belonging to the java.util.List interface.

2016, Marcus Biel, http://www.marcus-biel.com/

Collection Interface Hierarchy<<interface>

>Collection

Okay, so let’s start with the methods belonging to the java.util.Collection interface.

The contract of the collection interface does not guarantee any particular order and therefore does not provide any index or order related methods.

2016, Marcus Biel, http://www.marcus-biel.com/

boolean add(E e)boolean addAll(Collection<? extends E> c)boolean remove(Object o)boolean removeAll(Collection<?> c)

java.util.Collection

So here you can see the first set of methods that implement the collection interface.

So what I say about these methods does not only apply to ArrayList,

But also to all classes that implement the collection interface.

2016, Marcus Biel, http://www.marcus-biel.com/

boolean add(E e)boolean addAll(Collection<? extends E> c)boolean remove(Object o)boolean removeAll(Collection<?> c)

java.util.Collection

The method “boolean add” appends the element to the end of the collection to the next empty

cell of the underlying array.

2016, Marcus Biel, http://www.marcus-biel.com/

boolean add(E e)boolean addAll(Collection<? extends E> c)boolean remove(Object o)boolean removeAll(Collection<?> c)

java.util.Collection

boolean addAll – appends all given elements to the end of the collection.

2016, Marcus Biel, http://www.marcus-biel.com/

boolean add(E e)boolean addAll(Collection<? extends E> c)boolean remove(Object o)boolean removeAll(Collection<?> c)

java.util.Collection

The stuff in angle brackets is related to Generics. it ensures that no one can call such a method

with the wrong arguments. I will tell you more in my upcoming slides about Generics.

2016, Marcus Biel, http://www.marcus-biel.com/

boolean add(E e)boolean addAll(Collection<? extends E> c)boolean remove(Object o)boolean removeAll(Collection<?> c)

java.util.Collection

boolean remove – removes the first occurrence of the element

you specify from the collection.

2016, Marcus Biel, http://www.marcus-biel.com/

java.util.Collectionboolean add(E e)boolean addAll(Collection<? extends E> c)boolean remove(Object o)boolean removeAll(Collection<?> c)

boolean removeAll – removes the given elements from the Collection.

2016, Marcus Biel, http://www.marcus-biel.com/

Iterator<E> iterator() int size() boolean contains(Object o)

java.util.Collection

The iterator method returns an object you usually use in a loop to move from one element

to the next element of the collection, step by step. You say “I iterate over the collection” –

hence the name Iterator.

2016, Marcus Biel, http://www.marcus-biel.com/

java.util.CollectionIterator<E> iterator() int size() boolean contains(Object o)

int size() – returns the number of elements of the collection.

2016, Marcus Biel, http://www.marcus-biel.com/

Iterator<E> iterator() int size() boolean contains(Object o)

java.util.Collection

boolean contains - returns true if the collection contains at least

- one instance of the element you specify.

2016, Marcus Biel, http://www.marcus-biel.com/

void clear() boolean isEmpty() <T> T[] toArray(T[] a)

java.util.Collection

void clear() - removes all elements from the collection.

2016, Marcus Biel, http://www.marcus-biel.com/

void clear() boolean isEmpty() <T> T[] toArray(T[] a)

java.util.Collection

boolean isEmpty() – This returns true if the collection contains no elements.

2016, Marcus Biel, http://www.marcus-biel.com/

void clear() boolean isEmpty() <T> T[] toArray(T[] a)

java.util.Collection

and toArray – returns an array containing

all of the elements of the collection.

2016, Marcus Biel, http://www.marcus-biel.com/

Collection Interface Hierarchy<<interface>

>List

ArrayList

Let’s move on to the methods of the java.util.List interface. The methods are similar in part to the

methods we just looked at, but they differ in that they require

an order on the elements of the list.

2016, Marcus Biel, http://www.marcus-biel.com/

java.util.Listboolean add(int index, E element)E remove(int index) 

So here again you should know that everything I say about these methods

does not only apply to ArrayList but to all classes that implement the list interface.

2016, Marcus Biel, http://www.marcus-biel.com/

java.util.Listboolean add(int index, E element)E remove(int index) 

This add method with an index parameter acts actually more like an insert method.

It allows you to insert an element at any index position of the list,

instead of just adding the element to the end of the list. In the process, the elements of the underlying array

will be shifted to the right and migrated to a larger array if necessary.

2016, Marcus Biel, http://www.marcus-biel.com/

java.util.Listboolean add(int index, E element)E remove(int index) 

The remove index method allows to remove an element from any index position of the list.

Similar to the add method we just looked at, this might require to shift the remaining elements

of the underlying array to the left.

2016, Marcus Biel, http://www.marcus-biel.com/

E get(int index) int indexOf(Object o) int lastIndexOf(Object o)

java.util.List

The get by index method returns an element from any given position of the list.

2016, Marcus Biel, http://www.marcus-biel.com/

java.util.ListE get(int index) int indexOf(Object o) int lastIndexOf(Object o)

The indexOf method takes an object and returns the index of the first occurrence of the

element in the list or “-1” if the element is not found.

2016, Marcus Biel, http://www.marcus-biel.com/

java.util.ListE get(int index) int indexOf(Object o) int lastIndexOf(Object o)

int lastIndexOf returns the index of the last occurrence of the element in the list and

as before, “-1” if the element is not found.

2016, Marcus Biel, http://www.marcus-biel.com/

java.util.ListList<E> subList(int fromIndex, int toIndex) void sort(Comparator<? super E> c)

List subList returns a view of the list starting with the position you specify

as fromIndex and ending one position before the one you specify as toIndex.

2016, Marcus Biel, http://www.marcus-biel.com/

java.util.ListList<E> subList(int fromIndex, int toIndex) void sort(Comparator<? super E> c)

Last but not least, the sort method sorts the list following

the order of the given Comparator.

2016, Marcus Biel, http://www.marcus-biel.com/

Copyright © 2016 Marcus BielAll rights reserved