Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists What to do?. Lists  A list is a linear arrangement of data elements.  Items are arranged in sequential (linear) order  Items are therefore ordered.

Similar presentations


Presentation on theme: "Lists What to do?. Lists  A list is a linear arrangement of data elements.  Items are arranged in sequential (linear) order  Items are therefore ordered."— Presentation transcript:

1 Lists What to do?

2 Lists  A list is a linear arrangement of data elements.  Items are arranged in sequential (linear) order  Items are therefore ordered (before-after relationship)  A SimpleList  Each item is an Object.  Can add items to and remove items from the list.  A cursor location is part of the state of a SimpleList.  The cursor is always between two items (unless at the start or end).  When first constructed, the list is empty and the cursor is at the start. 2

3 SimpleList Class Diagram  add: the item is placed at the cursor position and the cursor is advanced past the newly inserted item  remove: the item immediately preceding the cursor is removed from the list. A call to next must have been made prior to removal.  next: returns the item immediately following the cursor and advances the cursor by one element.  hasNext: returns true if there is an item following the cursor  reset: places the cursor at the beginning of the list  size: returns the number of items in the list. SimpleList + void add(ItemType) + void remove() + ItemType next() + boolean hasNext() + int size() + void reset() SimpleLists are generic. When constructed, the item type must be provided. The item type cannot be a primitive type. 3

4 Example SimpleList list = new SimpleList (); String str; list.add( “abc” ); list.add( “def” ); list.reset(); str = list.next(); list.add( “xyz” ); str = list.next(); list.remove(); list.add( “123” ); list.reset(); list.add( “rst” ); list.reset(); while ( list.hasNext() ) { System.out.println( list.next() ); } list.add( “uvw” ); SimpleList list = new SimpleList (); String str; list.add( “abc” ); list.add( “def” ); list.reset(); str = list.next(); list.add( “xyz” ); str = list.next(); list.remove(); list.add( “123” ); list.reset(); list.add( “rst” ); list.reset(); while ( list.hasNext() ) { System.out.println( list.next() ); } list.add( “uvw” ); 4

5 SimpleList pattern  Most list processing code will look like:  Write a method that takes a simple list of strings and returns the shortest one. list.reset(); while(list.hasNext()) { Object item = list.next(); // process the item } list.reset(); while(list.hasNext()) { Object item = list.next(); // process the item } 5

6 SimpleList  SimpleLists are generic  The type of the items in the list must be specified.  The items in the array must be Objects  Can we have a simple list of ‘ints’?  Primitives don’t conform to Object!  Must use “wrappers”. A wrapper class is meant to contain a single primitive datum SimpleList list = new SimpleList (); list.add(3); SimpleList list = new SimpleList (); list.add(3); 6

7 Wrapper classes PrimitiveWrapperto Wrapper (variable is a primitive ) to Primitive (variable is a wrapper ) byteBytenew Byte(b)b.byteValue() shortShortnew Short(s)s.shortValue() intIntegernew Integer(i)i.intValue() longLongnew Long(l)l.longValue() floatFloatnew Float(f)f.floatValue() doubleDoublenew Double(d)d.doubleValue() charCharacternew Character(c)c.charValue() booleanBooleannew Boolean(b)b.booleanValue() 7

8 Wrappers and SimpleList  Write a method to create and return a SimpleList containing N randomly generated doubles in the range 0 to 1. N is a non-negative integer. public SimpleList randomList(int n) { SimpleList doubles = new SimpleList (); for(int i=0; i<n; i++) { doubles.add(new Double(Math.random())); } return doubles; } public SimpleList randomList(int n) { SimpleList doubles = new SimpleList (); for(int i=0; i<n; i++) { doubles.add(new Double(Math.random())); } return doubles; } 8

9 Wrappers and SimpleList  Write a method that accepts a simple list of integers and computes the sum. The input list must not be changed. public int sum(SimpleList values) { int sum = 0; values.reset(); while(values.hasNext()) { Integer val = values.next(); sum = sum + val.intValue(); } return sum; } public int sum(SimpleList values) { int sum = 0; values.reset(); while(values.hasNext()) { Integer val = values.next(); sum = sum + val.intValue(); } return sum; } 9

10 Autoboxing  Since converting between primitives and wrapper classes is so common, Java performs this conversion automatically when necessary.  Auto boxing: automatic conversion between a primitive and it’s corresponding wrapper type  Auto Unboxing: automatic conversion between a wrapper and it’s corresponding primitive type 10

11 Boxing/Unboxing examples  Rewrite the randomList and sum functions public SimpleList randomList(int n) { SimpleList doubles = new SimpleList (); for(int i=0; i<n; i++) { doubles.add(Math.random()); } return doubles; } public SimpleList randomList(int n) { SimpleList doubles = new SimpleList (); for(int i=0; i<n; i++) { doubles.add(Math.random()); } return doubles; } public int sum(SimpleList values) { int sum = 0; values.reset(); while(values.hasNext()) { sum = sum + values.next(); } return sum; } public int sum(SimpleList values) { int sum = 0; values.reset(); while(values.hasNext()) { sum = sum + values.next(); } return sum; } 11

12 Specialized for loops  Since processing the items in a collection is also common, Java supports a special loop for collections for( ItemType itemName : collectionReference ) { // process the variable ‘itemName’ } for( ItemType itemName : collectionReference ) { // process the variable ‘itemName’ } collectionReference: a variable of type Collection (not yet defined) ItemType: a class name that must conform to the declared type of the elements in the collection itemName: the name of the item being processed in the loop body collectionReference: a variable of type Collection (not yet defined) ItemType: a class name that must conform to the declared type of the elements in the collection itemName: the name of the item being processed in the loop body The list is reset. Every item in the list is given the name ‘itemName’ one at a time as it is processed The cursor is placed at the end of the list when the loop is completed. The loop body must not alter the list (no adds or removes) The list is reset. Every item in the list is given the name ‘itemName’ one at a time as it is processed The cursor is placed at the end of the list when the loop is completed. The loop body must not alter the list (no adds or removes) 12

13 Example  Rewrite the sum function public int sum(SimpleList values) { int sum = 0; for(Integer val : values) { sum += val; } return sum; } public int sum(SimpleList values) { int sum = 0; for(Integer val : values) { sum += val; } return sum; } public int sum(SimpleList values) { int sum = 0; values.reset(); while(values.hasNext()) { sum = sum + values.next(); } return sum; } public int sum(SimpleList values) { int sum = 0; values.reset(); while(values.hasNext()) { sum = sum + values.next(); } return sum; } 13

14 More examples  Consider the following problems:  Write a method that takes a list of integers and squares each value.  Write a method that copies a list of strings  Write a method that accepts a list of integers and returns the smallest one. Return 0 if the list is of size 0. 14 SimpleList + void add(ItemType) + void remove() + ItemType next() + boolean hasNext() + int size() + void reset()


Download ppt "Lists What to do?. Lists  A list is a linear arrangement of data elements.  Items are arranged in sequential (linear) order  Items are therefore ordered."

Similar presentations


Ads by Google