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.

Slides:



Advertisements
Similar presentations
Hip Hip Array! AP Computer Science. Remember Strings? Strings are an array of characters An array is a collection of variables all of the same type. Arrays.
Advertisements

CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Chapter 7 – Arrays.
Computer Science A 10: 20/3. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[]
1 Java intro Part 3. 2 Arrays in Java Store fixed number of values of a given type Arrays are objects –have attributes –must be constructed Array declaration:
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 11 Arrays Continued
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Copyright 2008 by Pearson Education Building Java Programs Chapter 10 Lecture 10-1: ArrayList reading: 10.1.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
ArrayLists (and the for-each loop) ArrayList example = new ArrayList (); example.add(4);
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
int [] scores = new int [10];
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Basics.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
ARRAYS Multidimensional realities Image courtesy of
The ArrayList Data Structure Standard Arrays at High Speed!
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Sixth Lecture ArrayList Abstract Class and Interface
Lecture 20: Wrapper Classes and More Loops
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Intro to Collections.
Computer Programming Methodology Input and While Loop
Can store many of the same kind of data together
ArrayLists.
Chapter 2: Basic Elements of Java
int [] scores = new int [10];
Arrays and Collections
Chapter 7 The Java Array Object © Rick Mercer.
int [] scores = new int [10];
Lecture 13: ArrayLists AP Computer Science Principles
Can store many of the same kind of data together
slides created by Alyssa Harding
Presentation transcript:

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.  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

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

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

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

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

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

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

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

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

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

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

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

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 SimpleList + void add(ItemType) + void remove() + ItemType next() + boolean hasNext() + int size() + void reset()