Docsity

Array - Java - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Array - Java - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Programming Language – Java

Containers

 Hold a group of objects  Significantly increase your programming power  All perform bound checking  array: efficient, can hold primitives  Collection: a group of individual elements List, Set  Map: a group of key-value object pairs HashMap  Misleading: sometimes the whole container libraries are also called collection classes

Other containers

Can only take object Have to “wrap” primitives int - > Integer, double-> Double Have to cast or unwrap on retrieval Slow, error prone, tedious…. Fixed by JDK1.5, hopefully Advantage: automatic expanding

Arrays class

In java.util, a “wrapper” class for array A set of static utility methods fill(): fill an array with a value equals(): compare two arrays for equality sort(): sort an array binarySearch(): find one element in a sorted array All these methods overload for all primitive types and Object

Arrays.sort() cont.

sort(array), sort(array, fromIndex, toIndex) All elements in the array must implement the Comparable interface sort(array, comparator) sort(array, fromIndex, toIndex, comparator) All elements in the array must be mutually comparable by the specified comparator

Comparable interface

With a single method compareTo() Takes another Object as argument And returns: Negative value if this is less than argument Zero value if this is equal to argument positive value if this is greater than argument

Sort example: WorkerTest.java

Array.binarySearch()

Only usable on sorted array! Otherwise, result unpredictable If there are multiple elements equal to the specified object, there is no guarantee which one will be found. Return: Location if find the key (positive number)  (-( insertion point ) - 1) if not find key (negative)

Collection List ArrayList LinkedList Vector Set HashSet LinkedHashSet TreeSet Collection: hold one item at each location List: items in order Set: no duplicates, no ordering Preserve the insertion of the elements

Map HashMap LinkedHashMap Hashtable TreeMap Map: key-value pairs, fast retrieval no duplicate keys, no ordering Preserve the insertion of the elements

ArrayList

An array that automatically expand itself Put objects using add() Get out using get(int index) Need to cast type Method size() to get the number of objects Similar to .length attribute of array Example: CatsAndDogs.java

Iterator object

Access method regardless of the underlying structure Generic programming Can change underlying structure easily “light-weight” object Cheap to create Can move in only one direction

ArrayList vs. LinkedList

ArrayList Rapid random access Slow when inserting or removing in the middle LinkedList Optimal sequential access Fast insertion and deletion from the middle addFirst(), addLast(), getFirst(), removeFirst() Easy to be used as queue, stack

Set interface

Each element add to the Set must be unique , otherwise won’t add. Objects added to Set must define equals() to establish object uniqueness Not maintain order