+ All Categories
Home > Education > Java ArrayList Video Tutorial

Java ArrayList Video Tutorial

Date post: 15-Jan-2017
Category:
Upload: marcus-biel
View: 495 times
Download: 1 times
Share this document with a friend
53
2016, Marcus Biel, http://www.marcus-biel.com/ ArrayList Marcus Biel, Software Craftsman http:// www.marcus-biel.com
Transcript
Page 1: Java ArrayList Video Tutorial

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

ArrayListMarcus Biel, Software Craftsman

http://www.marcus-biel.com

Page 2: Java ArrayList Video Tutorial

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.

Page 3: Java ArrayList Video Tutorial

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.

Page 4: Java ArrayList Video Tutorial

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.

Page 5: Java ArrayList Video Tutorial

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.

Page 6: Java ArrayList Video Tutorial

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.

Page 7: Java ArrayList Video Tutorial

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.

Page 8: Java ArrayList Video Tutorial

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

SizeCapacity

Terminology

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

Page 9: Java ArrayList Video Tutorial

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.

Page 10: Java ArrayList Video Tutorial

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.

Page 11: Java ArrayList Video Tutorial

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

ArrayList Capacity…15

…10

First one array that can hold fifteen elements …

Page 12: Java ArrayList Video Tutorial

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

ArrayList Capacity

…22…15

…10

…then one for a maximum of twenty two elements…

Page 13: Java ArrayList Video Tutorial

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

ArrayList Capacity

…22…15

…10

…33

…then arrays with a capacity of thirty three…

Page 14: Java ArrayList Video Tutorial

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

ArrayList Capacity

…49

…22…15

…10

…33

…forty nine…

Page 15: Java ArrayList Video Tutorial

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

ArrayList Capacity

…73…49

…22…15

…10

…33

…seventy three…

Page 16: Java ArrayList Video Tutorial

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.

Page 17: Java ArrayList Video Tutorial

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.

Page 18: Java ArrayList Video Tutorial

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.

Page 19: Java ArrayList Video Tutorial

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.

Page 20: Java ArrayList Video Tutorial

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!

Page 21: Java ArrayList Video Tutorial

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.

Page 22: Java ArrayList Video Tutorial

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.

Page 23: Java ArrayList Video Tutorial

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.

Page 24: Java ArrayList Video Tutorial

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.

Page 25: Java ArrayList Video Tutorial

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.

Page 26: Java ArrayList Video Tutorial

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.

Page 27: Java ArrayList Video Tutorial

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…

Page 28: Java ArrayList Video Tutorial

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..

Page 29: Java ArrayList Video Tutorial

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.

Page 30: Java ArrayList Video Tutorial

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.

Page 31: Java ArrayList Video Tutorial

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.

Page 32: Java ArrayList Video Tutorial

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.

Page 33: Java ArrayList Video Tutorial

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.

Page 34: Java ArrayList Video Tutorial

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.

Page 35: Java ArrayList Video Tutorial

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.

Page 36: Java ArrayList Video Tutorial

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.

Page 37: Java ArrayList Video Tutorial

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.

Page 38: Java ArrayList Video Tutorial

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.

Page 39: Java ArrayList Video Tutorial

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.

Page 40: Java ArrayList Video Tutorial

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.

Page 41: Java ArrayList Video Tutorial

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.

Page 42: Java ArrayList Video Tutorial

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.

Page 43: Java ArrayList Video Tutorial

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.

Page 44: Java ArrayList Video Tutorial

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.

Page 45: Java ArrayList Video Tutorial

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.

Page 46: Java ArrayList Video Tutorial

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.

Page 47: Java ArrayList Video Tutorial

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.

Page 48: Java ArrayList Video Tutorial

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.

Page 49: Java ArrayList Video Tutorial

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.

Page 50: Java ArrayList Video Tutorial

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.

Page 51: Java ArrayList Video Tutorial

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.

Page 52: Java ArrayList Video Tutorial

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.

Page 53: Java ArrayList Video Tutorial

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

Copyright © 2016 Marcus BielAll rights reserved


Recommended