+ All Categories
Home > Documents > © A+ Computer Science - . Arraylist is a class that houses an array. It is internally implemented...

© A+ Computer Science - . Arraylist is a class that houses an array. It is internally implemented...

Date post: 21-Dec-2015
Category:
Upload: hugo-king
View: 220 times
Download: 0 times
Share this document with a friend
Popular Tags:
72
© A+ Computer Science - www.apluscompsci.com
Transcript

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

Arraylist is a class that houses anarray. It is internally implemented in Java as an array.

An ArrayList can store any type. It can store a combination of types.

All ArrayLists store the first referenceat index/position/subscript 0.

© A+ Computer Science -

www.apluscompsci.com

0 1 2 3 4 5 6 7 8 9

nums 0 0 0 0 0 0 0 0 0 0

int[] nums = new int[10]; //Java int array

An array is a group of items all of the same type which are accessed through a single identifier.

© A+ Computer Science -

www.apluscompsci.com

ArrayList is a descendant of List (which extends Collection), but because List and Collection are interfaces, you cannot instantiate them.

Collection bad = new Collection(); //illegal

List ray = new ArrayList(); //legalArrayList list = new ArrayList();//legal

ray and list store Object references.

© A+ Computer Science -

www.apluscompsci.com

list

nothing

null

null

ArrayList list;

list is a reference to an ArrayList.

© A+ Computer Science -

www.apluscompsci.com

0x213

new ArrayList();

ArrayLists are Objects.

[]

© A+ Computer Science -

www.apluscompsci.com

list0x213

0x213

ArrayList list = new ArrayList();

list is a reference to an ArrayList.

[]

© A+ Computer Science -

www.apluscompsci.com

Example of an ArrayList withoutthe type <T> identifier:

List ray = new ArrayList(); ray.add("hello");ray.add(“school");ray.add("contests");System.out.println(( (String) ray.get(0)).substring(0,1));System.out.println(( (String) ray.get(2)). substring(0,1));

ray stores Object references.

OUTPUT

hc

casting (must cast in order to send messages to the object)

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

Generic ArrayLists are array lists that take a type parameter. In other words you identify the type of all the elements in the array list.

© A+ Computer Science -

www.apluscompsci.com

With Java 5, you can now specify whichtype of reference you want to store in the ArrayList.

ArrayList<String> words;words = new ArrayList<String>();

ArrayList<Double> decNums;decNums = new ArrayList<Double>();

© A+ Computer Science -

www.apluscompsci.com

With Java 5, you can now specify whichtype of reference you want to store in the ArrayList.

ArrayList<Integer> bigStuff;bigStuff = new ArrayList<Integer>();

ArrayList<Student> studentList;studentList = new ArrayList<Student>();

© A+ Computer Science -

www.apluscompsci.com

List<String> ray;ray = new ArrayList<String>(); ray.add("hello");ray.add(“school");ray.add("contests");System.out.println(ray.get(0).substring(0,1));System.out.println(ray.get(2).substring(0,1)); ray stores String

references.

OUTPUT

hc

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

ArrayListfrequently used methods

Name Useadd(item) adds item to the end of the list

add(index,item) adds item at index – shifts items up->

set(index,item) replaces item at index (returns old element) (similar to z[index]=item)

get(index) returns the item at index (similar to z[index] )

size() returns the # of items in the list (similar to z.length)

remove(index) removes the item at index (returns element that was removed)

remove(value) removes first item that matches this value (returns true if item was found, else false)

clear() removes all items from the list

import java.util.ArrayList;

© A+ Computer Science -

www.apluscompsci.com

ArrayList<String> words;words = new ArrayList<String>();

words.add(0,"it");words.add("is");words.add("a");words.add("lie");System.out.println(words);

OUTPUT

[it, is, a, lie]

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Integer> nums;nums = new ArrayList<Integer>();

nums.add(34);nums.add(0,99);nums.add(21);nums.add(11);System.out.println(nums);

OUTPUT

[99, 34, 21, 11]

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();

ray.add(23);ray.add(11);ray.set(0,66);ray.add(53);ray.set(1,93);ray.add(22);System.out.println(ray);

OUTPUT

[66, 93, 53, 22]

© A+ Computer Science -

www.apluscompsci.com

for (int i=0; i<ray.size(); i++){ System.out.println(ray.get(i)); }

.size( ) returns the number of elements (logical/physical size) in the array list.

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Double> ray;ray = new ArrayList<Double>();ray.add(23.23);ray.add(11.11);ray.add(12.1);ray.add(65.6);

System.out.println(ray.get(0));System.out.println(ray.get(3));

OUTPUT23.2365.6

.get(index) returns the reference stored at the index!

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();ray.add(23);ray.add(11);ray.add(12);ray.add(65);

for(int i=0; i<ray.size(); i++) System.out.println(ray.get(i));

OUTPUT23111265

.get(index) returns the reference stored at the index!

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();ray.add(23);ray.add(11);ray.add(12);ray.add(65);

for( int val : ray ) System.out.println(val);

OUTPUT23111265

val receives one value from ray each iteration.

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

ArrayList<String> ray;ray = new ArrayList<String>();

ray.add("a");ray.add("b");ray.remove(0);ray.add("c");ray.add("d");ray.remove(0);System.out.println(ray);

OUTPUT

[c, d]

© A+ Computer Science -

www.apluscompsci.com

ArrayList<String> ray;ray = new ArrayList<String>();

ray.add("a");ray.add("b");ray.remove("a");ray.add("c");ray.add("d");ray.remove("d");System.out.println(ray);

OUTPUT

[b, c]

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

ArrayList<String> ray;ray = new ArrayList<String>();

ray.add("a");ray.add("x");ray.clear();ray.add("t");ray.add("w");System.out.println(ray);

OUTPUT

[t, w]

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

How can you initialize an ArrayList?

ArrayList<String> names = new ArrayList<String>();names.add(“Smith”);names.add(“Jones”);

Or similar to an initializer list for an array:

ArrayList<String> names = new ArrayList<String>(Arrays.asList("Smith", "Jones"));

import java.util.ArrayList;import java.util.Arrays;

© A+ Computer Science -

www.apluscompsci.com

• Purpose: To get familiar with ArrayLists

• Make a copy of your EmployeeNames and EmployeeNamesTester and name them EmployeeArrayList and EmployeeArrayListTester.

• Change all occurrences of arrays to ArrayLists.

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();

ray.add(23);ray.add(11);ray.add(53);

for(int num : ray){ System.out.println(num);}

OUTPUT

231153

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

Primitive typesObjects

(wrapper class)byte Byte

short Short

int Integer

long Long

float Float

double Double

char Character

boolean Boolean

== .equals()

© A+ Computer Science -

www.apluscompsci.com

Before Java 5 added in autoboxingand autounboxing, you had tomanually wrap primitives.

Integer x = new Integer(98);orint y = 56;x= new Integer(y);

© A+ Computer Science -

www.apluscompsci.com

Java now wraps automatically.

Integer num1 = 99;andInteger num1 = new Integer(99);

These two lines are equivalent.

© A+ Computer Science -

www.apluscompsci.com

Java now wraps automatically.

Double num1 = 99.1;andDouble num1 = new Double(99.1);

These two lines are equivalent.

© A+ Computer Science -

www.apluscompsci.com

Before Java 5 added in autoboxingand autounboxing, you had tomanually unwrap references.

If you boxed 98 as an Integer like this:Integer ref = new Integer(98);

Then you had to unbox it like this:int y = ref.intValue();

© A+ Computer Science -

www.apluscompsci.com

OUTPUT

33

Java now unwraps automatically.

int prim;Integer num = new Integer(3);

OLD WAY:prim=num.intValue(); System.out.println(prim);

NEW WAY:prim = num;System.out.println(prim);

These two lines are equivalent.

© A+ Computer Science -

www.apluscompsci.com

Double dub = 9.3;double prim = dub;System.out.println(prim);

int num = 12;Integer big = num;System.out.println(big.compareTo(12));System.out.println(big.compareTo(17));System.out.println(big.compareTo(10));

OUTPUT

9.30-11

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();

//add some values to rayint total = 0;for(Integer num : ray){ //this line shows the AP preferred way //it shows the manual retrieval of the primitive total = total + num.intValue(); // total += num.intValue();

//the line below accomplishes the same as the line above //but it uses autounboxing to get the primitive value total = total + num; // total += num;}System.out.println(total);

OUTPUT153

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

public class Creature implements Comparable{ private int size;

public Creature(int girth) { size=girth; }

public void setSize(int girth) { size=girth; }

public String toString() { return “” + size; }

//Other methods not shown here}

Instance variableInstance variable

Constructor initializes size of Creature

Constructor initializes size of Creature

toString() returns the String representation of

Creature

toString() returns the String representation of

Creature

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Creature> creatureList;creatureList = new ArrayList<Creature>();creatureList.add(new Creature(4));creatureList.add(new Creature(9));creatureList.add(new Creature(1));

creatureList 0x12 0x32 0xD2

0 1 2

Creature 4

Creature 9

Creature 1

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Creature> creatureList;creatureList = new ArrayList<Creature>();creatureList.add(new Creature(4));creatureList.add(new Creature(9));creatureList.add(new Creature(1));

System.out.println(creatureList.get(0));

creatureList.get(0).setSize(7);System.out.println(creatureList.get(0));

System.out.println(creatureList.get(2));

OUTPUT471

© A+ Computer Science -

www.apluscompsci.com

creatureList.get(0)

What does the . dot do?

setSize(7);.

What does this return?

Creature

0x242

0x242

The . dot grants access to the Object at the stored address.

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

Ends with an sEnds with an s

© A+ Computer Science -

www.apluscompsci.com

Collectionsfrequently used methods

Name Usesort(x) Puts all items in array list x in ascending

order

binarySearch(x,y) Prerequisite: array list x must be sorted. Checks x for the location of y. This method returns the index where y is found. If y is not found, it returns -1 minus (the position in the array list where x would be added).

fill(x,y) Fills all elements in x with value y

rotate(x,y) Shifts items in x left or right y locations

reverse(x) Reverses the order of the items in x

import java.util.Collections;

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();

ray.add(23);ray.add(11);ray.add(66);ray.add(53);Collections.sort(ray);

System.out.println(ray);System.out.println(Collections.binarySearch(ray,677));System.out.println(Collections.binarySearch(ray,66));

OUTPUT[11, 23, 53, 66]-53

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Integer> ray = ArrayList<Integer>();

ray.add(23);ray.add(11);ray.add(53);ray.add(100);System.out.println(ray);

Collections.rotate(ray,2);System.out.println(ray);

Collections.rotate(ray,2); // back to [23, 11, 53, 100]Collections.reverse(ray);System.out.println(ray);

OUTPUT[23, 11, 53, 100][53, 100, 23, 11][100, 53, 11, 23]

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();ray.add(0);ray.add(20);ray.add(50);System.out.println(ray);

Collections.fill(ray,33);System.out.println(ray);

OUTPUT[0, 20, 50][33, 33, 33]

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

ArrayListfrequently used methods

Name Usecontains(y) Checks to see if the list contains y.

Returns either true or false.

indexOf(y) Checks the array list for the location of y. Returns the index where y is found. If not found, it returns -1.

isEmpty() Returns true if the array list is empty (no elements) or false if it contains elements.

© A+ Computer Science -

www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();

ray.add(23);ray.add(11);ray.add(66);ray.add(53);

System.out.println(ray);System.out.println(ray.indexOf(21));System.out.println(ray.indexOf(66));

System.out.println(ray);System.out.println(ray.contains(21));System.out.println(ray.contains(66));

OUTPUT[23, 11, 66, 53]-12[23, 11, 66, 53]falsetrue

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

© A+ Computer Science -

www.apluscompsci.com

The following are important interfaces included in the Java language:

CollectionList

© A+ Computer Science -

www.apluscompsci.com

Collection

Map

List Set

Sub Interfaces-extends

Implementing Classes

ArrayListLinkedListVectorStack AbstractSet

HashSetLinkedHashSet

TreeSet

SortedSet

Sub Interfaces-extends

Implementing Classes

TreeMap

SortedMap

Sub Interfaces-extends

Implementing Classes

Implementing Classes

HashMapHashTable

© A+ Computer Science -

www.apluscompsci.com

The Collection interface is the parent of Listand Set. The Collection interface has manymethods including add(), clear(), remove(), and size().

Collection

List Set

others not shown

© A+ Computer Science -

www.apluscompsci.com

The List interface extends the Collection interface. Additionally, the List interface

also has the get() method as well as several other methods.

List

ArrayList LinkedList

others not shown

© A+ Computer Science -

www.apluscompsci.com

ArrayList is a descendant of List and Collection, but because List and Collection are interfaces, you cannot instantiate them.

Collection bad = new Collection(); //illegalList justAsBad = new List(); //illegal

List list = new ArrayList(); //legalArrayList aList = new ArrayList(); //legalCollection col = new ArrayList(); //legal **** Legal only if using methods from Collection interfacelist, aList, and col store Object references.

© A+ Computer Science -

www.apluscompsci.com


Recommended