Introduction to Computer Science I - Iterators ArrayList

20
Introduction to Computer Science I Iterators ArrayList Janyl Jumadinova 2 April, 2018

Transcript of Introduction to Computer Science I - Iterators ArrayList

Page 1: Introduction to Computer Science I - Iterators ArrayList

Introduction to Computer Science I

IteratorsArrayList

Janyl Jumadinova

2 April, 2018

Page 2: Introduction to Computer Science I - Iterators ArrayList

IteratorsI One of the most useful operations for any collection is the

ability to run through each of the elements in a loop.

I This process is called iteration.

2/13

Page 3: Introduction to Computer Science I - Iterators ArrayList

Iterator object

Methods:I hasNext(): returns a boolean value

true if there is at least one more item to process.

I next(): retrieves the next item in the collection to process.

3/13

Page 4: Introduction to Computer Science I - Iterators ArrayList

Iterators

Several classes in Java API define iterators.

I Scanner:hasNext(): returns true if there is another input token toprocess

4/13

Page 5: Introduction to Computer Science I - Iterators ArrayList

ArrayList

Write a program that reads a file and displays the words of that fileas a list.

I First display all words.

I Then display them with all plurals (ending in “s”) capitalized.

I Then display them in reverse order.

I Then display them with all plural words removed.

Problem: Don’t know how many words the file will have.

5/13

Page 6: Introduction to Computer Science I - Iterators ArrayList

ArrayList

Write a program that reads a file and displays the words of that fileas a list.

I First display all words.

I Then display them with all plurals (ending in “s”) capitalized.

I Then display them in reverse order.

I Then display them with all plural words removed.

Problem: Don’t know how many words the file will have.

5/13

Page 7: Introduction to Computer Science I - Iterators ArrayList

Collections

I Collection: an object that stores data; a.k.a. “data structure”

I The objects stored are called elements

I Some collections maintain an ordering; some allow duplicates

I Typical operations: add, remove, clear, contains (search), size

I Examples found in the Java class libraries: ArrayList,LinkedList, HashMap, TreeSet, PriorityQueue

I all collections are in the java.util package

6/13

Page 8: Introduction to Computer Science I - Iterators ArrayList

Collections

I Collection: an object that stores data; a.k.a. “data structure”

I The objects stored are called elements

I Some collections maintain an ordering; some allow duplicates

I Typical operations: add, remove, clear, contains (search), size

I Examples found in the Java class libraries: ArrayList,LinkedList, HashMap, TreeSet, PriorityQueue

I all collections are in the java.util package

6/13

Page 9: Introduction to Computer Science I - Iterators ArrayList

Collections

7/13

Page 10: Introduction to Computer Science I - Iterators ArrayList

ListsI List: a collection storing an ordered sequence of elements

I Each element is accessible by a 0-based index

I A list has a size (number of elements that have been added)

I Elements can be added to the front, back, or elsewhere

I In Java, a list can be represented as an ArrayList object

8/13

Page 11: Introduction to Computer Science I - Iterators ArrayList

ListsI List: a collection storing an ordered sequence of elements

I Each element is accessible by a 0-based index

I A list has a size (number of elements that have been added)

I Elements can be added to the front, back, or elsewhere

I In Java, a list can be represented as an ArrayList object

8/13

Page 12: Introduction to Computer Science I - Iterators ArrayList

ListsI List: a collection storing an ordered sequence of elements

I Each element is accessible by a 0-based index

I A list has a size (number of elements that have been added)

I Elements can be added to the front, back, or elsewhere

I In Java, a list can be represented as an ArrayList object

8/13

Page 13: Introduction to Computer Science I - Iterators ArrayList

ArrayList Methods

9/13

Page 14: Introduction to Computer Science I - Iterators ArrayList

ArrayList Methods

10/13

Page 15: Introduction to Computer Science I - Iterators ArrayList

Type Parameters (Generics)

I ArrayList<Type> name = new ArrayList<Type>();

I When constructing an ArrayList, you must specify the type ofelements it will contain between < and >.

I This is called a type parameter or a generic class.

I Allows the same ArrayList class to store lists of different types.

ArrayList<String> names = new ArrayList<String>();

names.add("Marty Stepp");

names.add("Stuart Reges");

11/13

Page 16: Introduction to Computer Science I - Iterators ArrayList

Type Parameters (Generics)

I ArrayList<Type> name = new ArrayList<Type>();

I When constructing an ArrayList, you must specify the type ofelements it will contain between < and >.

I This is called a type parameter or a generic class.

I Allows the same ArrayList class to store lists of different types.

ArrayList<String> names = new ArrayList<String>();

names.add("Marty Stepp");

names.add("Stuart Reges");

11/13

Page 17: Introduction to Computer Science I - Iterators ArrayList

ArrayList of Primitives?

I The type you specify when creating an ArrayList must be anobject type; it cannot be a primitive type.

// illegal -- int cannot be a type parameter

ArrayList<int> list = new ArrayList<int>();

I But we can still use ArrayList with primitive types by usingspecial classes called wrapper classes in their place.

// creates a list of ints

ArrayList<Integer> list = new ArrayList<Integer>();

12/13

Page 18: Introduction to Computer Science I - Iterators ArrayList

ArrayList of Primitives?

I The type you specify when creating an ArrayList must be anobject type; it cannot be a primitive type.

// illegal -- int cannot be a type parameter

ArrayList<int> list = new ArrayList<int>();

I But we can still use ArrayList with primitive types by usingspecial classes called wrapper classes in their place.

// creates a list of ints

ArrayList<Integer> list = new ArrayList<Integer>();

12/13

Page 19: Introduction to Computer Science I - Iterators ArrayList

ArrayList of Primitives?

I The type you specify when creating an ArrayList must be anobject type; it cannot be a primitive type.

// illegal -- int cannot be a type parameter

ArrayList<int> list = new ArrayList<int>();

I But we can still use ArrayList with primitive types by usingspecial classes called wrapper classes in their place.

// creates a list of ints

ArrayList<Integer> list = new ArrayList<Integer>();

12/13

Page 20: Introduction to Computer Science I - Iterators ArrayList

Wrapper Classes

I A wrapper is an object whose sole purpose is to hold a primitivevalue.

I Once you construct the list, use it with primitives as normal:

ArrayList<Double> grades = new ArrayList<Double>();

grades.add(3.2);

grades.add(2.7);

...

double myGrade = grades.get(0);

Example13/13