+ All Categories
Home > Documents > Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic...

Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic...

Date post: 17-Jun-2020
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
22
04 ArrayList Data Structures - Java ArrayList Leon Andretti Abdillah
Transcript
Page 1: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

04

ArrayList

Data Structures - Java ArrayList

Leon Andretti Abdillah

Page 2: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Introduction

A collection — sometimes called a container — is simply an

object that groups multiple elements into a single unit.

Collections are used to store, retrieve, manipulate, and

communicate aggregate data.

Typically, they represent data items that form a natural

group, such as a poker hand (a collection of cards), a mail

folder (a collection of letters), or a telephone directory (a

mapping of names to phone numbers).

27/04/20142 DS - LeonAbdillah

Page 3: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

What is collection framework? A collections framework is a unified architecture for representing and

manipulating collections. All collections frameworks contain the following:1. Interfaces: These are abstract data types that represent

collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.

2. Implementations: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.

3. Algorithms:These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality.

27/04/20143 DS - LeonAbdillah

Page 4: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Interfaces

The core collection interfaces encapsulate different types of

collections, which are shown in the figure below.

These interfaces allow collections to be manipulated

independently of the details of their representation.

Core collection interfaces are the foundation of the Java

Collections Framework.

As you can see in the following figure, the core collection

interfaces form a hierarchy.

27/04/20144 DS - LeonAbdillah

Page 5: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Collections

27/04/2014DS - LeonAbdillah5

A Collection represents a group of objects known as its

elements. The Collection interface is used to pass around

collections of objects where maximum generality is desired.

For example, by convention all general-purpose collection

implementations have a constructor that takes

a Collection argument. This constructor, known as a

conversion constructor, initializes the new collection to contain

all of the elements in the specified collection, whatever the

given collection's subinterface or implementation type. In

other words, it allows you to convert the collection's type.

Page 6: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Collection - ArrayList

27/04/2014DS - LeonAbdillah6

The ArrayList is a linear data structure, similar to the array,

but resizable.

Recall that a Collection is an object that holds a collection (or

a group, or a container) of elements. Below is an example of

using an ArrayList to hold a collection of String objects.

Page 7: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

27/04/2014DS - LeonAbdillah7

Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array after creating it, we prefer to use ArrayList in Java which re-size itself automatically once it gets full.

ArrayList in Java implements List interface and allow null. Java ArrayList also maintains insertion order of elements and allows duplicates opposite to any Set implementation which doesn't allow duplicates. ArrayListsupports both Iterator and ListIterator for iteration but it’s recommended to use ListIterator as it allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the Iterator's current position in the list. But while using ListIterator you need to be little careful because ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous () and the element that would be returned by a call to next ().

Read more: http://javarevisited.blogspot.com/2011/05/example-of-arraylist-in-java-tutorial.html#ixzz2zRiYI5xs

Page 8: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

ArrayList The ArrayList class extends AbstractList and implements the List

interface. ArrayList supports dynamic arrays that can grow as

needed.

Standard Java arrays are of a fixed length. After arrays are created,

they cannot grow or shrink, which means that you must know in

advance how many elements an array will hold.

Array lists are created with an initial size. When this size is

exceeded, the collection is automatically enlarged. When objects

are removed, the array may be shrunk.

The ArrayList class supports three constructors. The first

constructor builds an empty array list.

27/04/20148 DS - LeonAbdillah

Page 9: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

ArrayList’s methods - 1

27/04/2014DS - LeonAbdillah9

No Methods

1 void add(int index, Object element)

Inserts the specified element at the specified position index in this list. Throws

IndexOutOfBoundsException if the specified index is is out of range (index < 0

|| index > size()).

2 boolean add(Object o)

Appends the specified element to the end of this list.

3 boolean addAll(Collection c)

Appends all of the elements in the specified collection to the end of this list, in the

order that they are returned by the specified collection's iterator. Throws

NullPointerException if the specified collection is null.

4 boolean addAll(int index, Collection c)

Inserts all of the elements in the specified collection into this list, starting at the

specified position. Throws NullPointerException if the specified collection is null.

Page 10: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

ArrayList’s methods - 2

27/04/2014DS - LeonAbdillah10

continue

No Methods

5 Object set(int index, Object element)

Replaces the element at the specified position in this list with the specified

element. Throws IndexOutOfBoundsException if the specified index is is out of

range (index < 0 || index >= size()).

6 Object get(int index)

Returns the element at the specified position in this list. Throws

IndexOutOfBoundsException if the specified index is is out of range (index < 0

|| index >= size()).

7 int size()

Returns the number of elements in this list.

8 void clear()

Removes all of the elements from this list.

Page 11: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

ArrayList’s methods - 3

27/04/2014DS - LeonAbdillah11

continue

No Methods

9 int indexOf(Object o)

Returns the index in this list of the first occurrence of the specified element, or -1

if the List does not contain this element.

10 int lastIndexOf(Object o)

Returns the index in this list of the last occurrence of the specified element, or -1

if the list does not contain this element.

11 Object remove(int index)

Removes the element at the specified position in this list. Throws

IndexOutOfBoundsException if index out of range (index < 0 || index >=

size()).

12 protected void removeRange(int fromIndex, int toIndex)

Removes from this List all of the elements whose index is between fromIndex,

inclusive and toIndex, exclusive.

Page 12: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Java ArrayList Code 1/8

27/04/2014DS - LeonAbdillah12

import java.util.ArrayList;

import java.util.Iterator;

public class daArrayList {

public static void main(String[] args) {

// create an array list

ArrayList aList = new ArrayList();

System.out.println("Initial size of aList \t : " + aList.size());

Page 13: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Java ArrayList Code 2/8

27/04/2014DS - LeonAbdillah13

// add elements to the array list

aList.add("Apple");

aList.add("Blackberry");

aList.add("Guava");

aList.add("Orange");

// display the array list

System.out.println("Contents of aList \t : " +

aList);

Page 14: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Java ArrayList Code 3/8

27/04/2014DS - LeonAbdillah14

// Add an element into index=1

aList.add(1, "20");

System.out.println("\"20\" was inserted \t : " + aList);

System.out.println("Size of aList now \t : " + aList.size());

aList.add("Fanta");

aList.add("Sugar");

aList.add("iPhone");

// IndexOf

System.out.println("The index of \"C\" \t : " + aList.indexOf("C"));

Page 15: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Java ArrayList Code 4/8

27/04/2014DS - LeonAbdillah15

// Checking if an element is included to the list

boolean item = aList.contains("Apple");

System.out.println("ArrayList containt Apple?: " + item);

// Getting the element in a specific position

String getItem = aList.get(2);

System.out.println("The item in index 2 is \t : " + getItem);

// Checking if array list is empty

boolean check = aList.isEmpty();

System.out.println("Arraylist is empty? \t : " + check);

Page 16: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Java ArrayList Code 5/8

27/04/2014DS - LeonAbdillah16

// Remove elements from the array list

aList.remove("20");

System.out.println("Delete element of \"20\" \t : " + aList);

System.out.println("Size of aList now \t : " + aList.size());

aList.remove(4); // index

System.out.println("Delete the 5th item \t : " + aList);

System.out.println("Size of aList now \t : " + aList.size());

aList.remove(aList.size()-1); // remove the last element

System.out.println("Delete the last item \t : " + aList);

System.out.println("Size of aList now \t : " + aList.size());

Page 17: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Java ArrayList Code 6/8

27/04/2014DS - LeonAbdillah17

// display ArrayList vertically by using for

System.out.println("\nDisplay ArrayList

Vertically by using For");

for(int i=0; i<aList.size(); i++){

System.out.println(aList.get(i));

}

Page 18: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Java ArrayList Code 7/8

27/04/2014DS - LeonAbdillah18

// display ArrayList vertically by using Iterator

System.out.println("\nDisplay ArrayList

Vertically by using Iterator");

Iterator iterator = aList.iterator();

while(iterator.hasNext()){

String element = (String) iterator.next();

System.out.println(element);

}

Page 19: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Java ArrayList Code 8/8

27/04/2014DS - LeonAbdillah19

// clear

aList.clear();

System.out.println("\nContents of aList \t : "

+ aList);

} // main

} // class

Page 20: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Result 1/2

27/04/2014DS - LeonAbdillah20

Page 21: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

Result 2/2

27/04/2014DS - LeonAbdillah21

Page 22: Data Structures - Java ArrayList Leon Andretti Abdillah...Java ArrayList represents an automatic re-sizable array and used in place of array. Since we can not modify size of an array

References

27/04/2014DS - LeonAbdillah22

L. A. Abdillah. (2013). Algorithms & Programming. Available:

http://blog.binadarma.ac.id/mleonaa/teaching/programming

/algorithm-and-programming-2/

L. A. Abdillah. (2014). Data Structures & Algorithms. Available:

http://blog.binadarma.ac.id/mleonaa/teaching/programming

/data-structures/

http://www3.ntu.edu.sg/home/ehchua/programming/java/J5c_Col

lection.html

http://docs.oracle.com/javase/8/docs/api/java/util/ArrayLi

st.html


Recommended