1 / 14

Lists

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 (before-after relationship) A SimpleList Each item is an Object. Can add items to and remove items from the list.

suzuki
Download Presentation

Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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) • ASimpleList • Each item is anObject. • 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.

  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. SimpleLists are generic. When constructed, the item type must be provided. The item type cannot be a primitive type.

  4. Example SimpleList<String> list = new SimpleList<String>(); 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” );

  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 }

  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<int> list = new SimpleList<int>(); list.add(3);

  7. Wrapper classes

  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<Double> randomList(int n) { SimpleList<Double> doubles = new SimpleList<Double>(); for(inti=0; i<n; i++) { doubles.add(new Double(Math.random())); } return doubles; }

  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<Integer> values) { int sum = 0; values.reset(); while(values.hasNext()) { Integer val = values.next(); sum = sum + val.intValue(); } return sum; }

  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

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

  12. Specialized for loops • Since processing the items in a collection is also common, Java supports a special loop for collections for( ItemTypeitemName : 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 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)

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

  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.

More Related