Introduction to Computer Science I

Similar documents
Computational Expression

Building Java Programs

CSE 143 Lecture 4. ArrayList. Reading: slides created by Marty Stepp

Building Java Programs

CSE 143 Lecture 4. Preconditions

AP Computer Science. ArrayLists

Building Java Programs

Building Java Programs Chapter 10

Exercise. Building Java Programs Chapter 10. Naive solution. Collections

ArrayLists. Chapter 12.1 in Savitch

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

Using arrays to store data

Parsing JSON, Using Libraries, Java Collections, Generics. Slides adapted from Craig Zilles

Parsing JSON, Using Libraries, Java Collections, Generics

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Building Java Programs

Computational Expression

CS 302: Introduction to Programming in Java. Lecture 12

Today: Java Library Classes for lists. Iterators, ListIterators. CS61B Lecture #7. Last modified: Fri Sep 12 14:41: CS61B: Lecture #7 1

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

Computational Expression

Binghamton University. CS-140 Fall Chapter 7.7. Lists. Java Library Data Structures

Computational Expression

Building Java Programs

CS 106A, Lecture 19 ArrayLists

CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists

Computational Expression

Inf1-OP. Collections. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 6, School of Informatics

Lesson 26: ArrayList (W08D1)

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

What happens if someone new wants to join our awesome club?

CSE 373. Java Collection Framework. reading: Weiss Ch. 3, 4.8. slides created by Marty Stepp

Inf1-OP. Collections. Perdita Stevens, adapting earlier version by Ewan Klein. January 9, School of Informatics

Rigidity of arrays. Inf1-OP. ArrayList. ArrayList: Methods. Declaration. Collections

Adam Blank Lecture 2 Winter 2019 CS 2. Introduction to Programming Methods

First Name Last Name ID#

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS 106A, Lecture 27 Final Exam Review 1

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

After the code has executed, what is the capacity of list? What is its size? c

CS 106A, Lecture 27 Final Exam Review 1

CSE 331. Generics (Parametric Polymorphism)

All answers will be posted on web site, and most will be reviewed in class.

Working with arrays. ArrayLists. Abstraction. Arrays

ArrayLists. COMP1400 Week 8. Wednesday, 12 September 12

Computational Expression

Announcements/Follow-ups

Lecture 9: Lists. MIT-AITI Kenya 2005

Computer Science 210 Data Structures Siena College Fall 2018

Building Java Programs

Collections Questions

Computational Expression

Introduction to Computer Science I

Software 1 with Java. Recitation No. 6 (Collections)

CS 106A, Lecture 20 ArrayLists and HashMaps

Some examples and/or figures were borrowed (with permission) from slides prepared by Prof. H. Roumani. The Collection Framework

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

Array Based Lists. Collections

Building Java Programs

ABSTRACT DATA TYPES: COLLECTIONS, LISTS, SETS, MAP, QUEUES. Thursday, June 30, 2011

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

MIT AITI Lecture 18 Collections - Part 1

Name Section Number. CS210 Exam #4 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Lists using LinkedList

Unit5: Packages and abstraction. Prepared by: Dr. Abdallah Mohamed, AOU-KW Updated by Mrs. Malak EL-Amir AOU SAB Fall 14-15

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

CS Programming I: ArrayList

COLLECTIONS & ITERATORS cs2420 Introduction to Algorithms and Data Structures Spring 2015

01. Which of the following statement describes dynamic resizing as is applies to the ArrayList class?

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Java Collections Framework

CSSE 220 Day 8. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysAndLists from SVN

CSSE 220. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysListPractice from SVN

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

CITS1001 week 4 Grouping objects

CS 211: Using ArrayList, Implementing Arraylist

Java Collections Framework: Interfaces

Software 1 with Java. Java Collections Framework. Collection Interfaces. Online Resources. The Collection Interface

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

Flowcharts [15 points]

Implementation. (Mapping to Java) Jörg Kienzle & Alfred Strohmeier. COMP-533 Implementation

Fundamental language mechanisms

CS11 Java. Winter Lecture 8

Computer Science II (Spring )

CSE 143 Lecture 3. More ArrayList; object-oriented programming. reading: 10.1;

Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

CONTAİNERS COLLECTİONS

CSE 143. Computer Programming II

Inf1-OOP. Encapsulation. Object Oriented Programming. Encapsulation Accessing Data Immutability. Encapsulation and Collections.

Inf1-OOP. Encapsulation and Collections. Perdita Stevens, adapting earlier version by Ewan Klein. March 2, School of Informatics

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

BBM 102 Introduction to Programming II Spring 2017

BBM 102 Introduction to Programming II Spring 2017

CMSC 132: Object-Oriented Programming II

STUDENT LESSON A15 ArrayList

Dynamically sized arrays. Overview. The problem with arrays. Java library. The Java Library. Dynamically sized arrays. Normal Java arrays:

17. Java Collections. Organizing Data. Generic List in Java: java.util.list. Type Parameters ( Parameteric Polymorphism ) Data Structures that we know

Transcription:

Introduction to Computer Science I Iterators ArrayList Janyl Jumadinova October 31, 2016

Iterators One of the most useful operations for any collection is the ability to run through each of the elements in a loop. This process is called iteration. 2/13

Iterator object Methods: hasnext(): returns a boolean value true if there is at least one more item to process. next(): retrieves the next item in the collection to process. 3/13

Iterators Several classes in Java API define iterators. Scanner: hasnext(): returns true if there is another input token to process 4/13

ArrayList Write a program that reads a file and displays the words of that file as a list. First display all words. Then display them with all plurals (ending in s ) capitalized. Then display them in reverse order. Then display them with all plural words removed. 5/13

ArrayList Write a program that reads a file and displays the words of that file as a list. First display all words. Then display them with all plurals (ending in s ) capitalized. Then display them in reverse order. Then display them with all plural words removed. Problem: Don t know how many words the file will have. 5/13

Collections Collection: an object that stores data; a.k.a. data structure The objects stored are called elements Some collections maintain an ordering; some allow duplicates Typical operations: add, remove, clear, contains (search), size 6/13

Collections Collection: an object that stores data; a.k.a. data structure The objects stored are called elements Some collections maintain an ordering; some allow duplicates Typical operations: add, remove, clear, contains (search), size Examples found in the Java class libraries: ArrayList, LinkedList, HashMap, TreeSet, PriorityQueue all collections are in the java.util package 6/13

Collections 7/13

Lists List: a collection storing an ordered sequence of elements Each element is accessible by a 0-based index A list has a size (number of elements that have been added) Elements can be added to the front, back, or elsewhere 8/13

Lists List: a collection storing an ordered sequence of elements Each element is accessible by a 0-based index A list has a size (number of elements that have been added) Elements can be added to the front, back, or elsewhere In Java, a list can be represented as an ArrayList object 8/13

Lists List: a collection storing an ordered sequence of elements Each element is accessible by a 0-based index A list has a size (number of elements that have been added) Elements can be added to the front, back, or elsewhere In Java, a list can be represented as an ArrayList object 8/13

ArrayList Methods 9/13

ArrayList Methods 10/13

Type Parameters (Generics) ArrayList<Type> name = new ArrayList<Type>(); When constructing an ArrayList, you must specify the type of elements it will contain between < and >. This is called a type parameter or a generic class. Allows the same ArrayList class to store lists of different types. 11/13

Type Parameters (Generics) ArrayList<Type> name = new ArrayList<Type>(); When constructing an ArrayList, you must specify the type of elements it will contain between < and >. This is called a type parameter or a generic class. 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

ArrayList of Primitives? The type you specify when creating an ArrayList must be an object type; it cannot be a primitive type. 12/13

ArrayList of Primitives? The type you specify when creating an ArrayList must be an object type; it cannot be a primitive type. // illegal -- int cannot be a type parameter ArrayList<int> list = new ArrayList<int>(); 12/13

ArrayList of Primitives? The type you specify when creating an ArrayList must be an object type; it cannot be a primitive type. // illegal -- int cannot be a type parameter ArrayList<int> list = new ArrayList<int>(); But we can still use ArrayList with primitive types by using special classes called wrapper classes in their place. // creates a list of ints ArrayList<Integer> list = new ArrayList<Integer>(); 12/13

Wrapper Classes A wrapper is an object whose sole purpose is to hold a primitive value. 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); Example 13/13