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