CS3101-3 Programming Language-Java Fall 2004 Oct.6
CS3101-3 Programming Language – Java Fall 2004 Oct. 6
Containers O Hold a group of objects o Significantly increase your programming power OAll perform bound checking ● array:efficient,can hold primitives Collection:a group of individual elements OList,Set o Map:a group of key-value object pairs OHashMap o Misleading:sometimes the whole container libraries are also called collection classes
Containers l Hold a group of objects l Significantly increase your programming power l All perform bound checking l array: efficient, can hold primitives l Collection: a group of individual elements ¡List, Set l Map: a group of key-value object pairs ¡HashMap l Misleading: sometimes the whole container libraries are also called collection classes
array o Most efficient way to do random access o Size is fixed and cannot be changed for the lifetime oIf run out of space,have to create a new one and copy everything Advantage:can hold primitives
array lMost efficient way to do random access lSize is fixed and cannot be changed for the lifetime lIf run out of space, have to create a new one and copy everything lAdvantage: can hold primitives
Other containers ●Can only take object ●Have to“wrap”primitives Oint->Integer,double->Double o Have to cast or unwrap on retrieval ● Slow,error prone,tedious.... Fixed by JDK1.5,hopefully oAdvantage:automatic expanding
Other containers lCan only take object lHave to “wrap” primitives ¡int -> Integer, double-> Double lHave to cast or unwrap on retrieval lSlow, error prone, tedious…. lFixed by JDK1.5, hopefully lAdvantage: automatic expanding
Arrays class ●In java.util,a "wrapper”class for array OA set of static utility methods Ofill():fill an array with a value Oequals():compare two arrays for equality Osort():sort an array ObinarySearch():find one element in a sorted array O All these methods overload for all primitive types and Object
Arrays class lIn java.util, a “wrapper” class for array lA 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 lAll these methods overload for all primitive types and Object
Arrays.sort() ●6 Sorts the objects into ascending order,according to their natural ordering o This sort is guaranteed to be stable:equal elements will not be reordered as a result of the sort oYou can specify a range.The range to be sorted extends from index fromlndex,inclusive,to index tolndex,exclusive. oThe objects need to comparable or there is a special comparator
Arrays.sort() l Sorts the objects into ascending order, according to their natural ordering l This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort l You can specify a range. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. l The objects need to comparable or there is a special comparator
Arrays.sort()cont. sort(array),sort(array,fromlndex,tolndex) OAll elements in the array must implement the Comparable interface sort(array,comparator) sort(array,fromlndex,tolndex,comparator) oAll elements in the array must be mutually comparable by the specified comparator
Arrays.sort() cont. lsort(array), sort(array, fromIndex, toIndex) lAll elements in the array must implement the Comparable interface lsort(array, comparator) lsort(array, fromIndex, toIndex, comparator) lAll elements in the array must be mutually comparable by the specified comparator
Comparable interface oWith a single method compareTo() o Takes another Object as argument ●And returns: ONegative value if this is less than argument OZero value if this is equal to argument Opositive value if this is greater than argument
Comparable interface lWith a single method compareTo() lTakes another Object as argument lAnd 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
Comparator interface Two methods:compare(),equals() oOnly need to implement compare( o Takes two Object as argument: compare(Object o1,Object o2) ●And returns ONegative value if o1 is less than o2 OZero value if o1 is equal to o2 Opositive value if o1 is greater than o2
Comparator interface lTwo methods: compare(), equals() lOnly need to implement compare() lTakes two Object as argument: compare(Object o1, Object o2) lAnd returns ¡Negative value if o1 is less than o2 ¡Zero value if o1 is equal to o2 ¡positive value if o1 is greater than o2
Sort example:WorkerTest.java
Sort example: WorkerTest.java