Computational Expression

Similar documents
Introduction to Computer Science I

Building Java Programs

AP Computer Science. ArrayLists

Building Java Programs

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

CSE 143 Lecture 4. Preconditions

Building Java Programs

ArrayLists. Chapter 12.1 in Savitch

Building Java Programs Chapter 10

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

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

Using arrays to store data

Computational Expression

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

Computational Expression

CS 302: Introduction to Programming in Java. Lecture 12

Computational Expression

Computational Expression

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

Building Java Programs

Computational Expression

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

Building Java Programs

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

CS 106A, Lecture 19 ArrayLists

Introduction to Computer Science I

First Name Last Name ID#

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

CSE 331. Generics (Parametric Polymorphism)

Working with arrays. ArrayLists. Abstraction. Arrays

ArrayLists. COMP1400 Week 8. Wednesday, 12 September 12

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

Announcements/Follow-ups

Lecture 9: Lists. MIT-AITI Kenya 2005

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

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

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

Building Java Programs

CS 106A, Lecture 27 Final Exam Review 1

CS 106A, Lecture 27 Final Exam Review 1

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

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

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

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

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

Computer Science 210 Data Structures Siena College Fall 2018

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

Computational Expression

MIT AITI Lecture 18 Collections - Part 1

CS 106A, Lecture 20 ArrayLists and HashMaps

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

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

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

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

Lesson 26: ArrayList (W08D1)

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Building Java Programs

CS 211: Using ArrayList, Implementing Arraylist

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

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

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

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

Lists using LinkedList

CSE 143. Computer Programming II

CS Programming I: ArrayList

BBM 102 Introduction to Programming II Spring 2017

BBM 102 Introduction to Programming II Spring 2017

Collections Questions

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

CMSC 132: Object-Oriented Programming II

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

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

STUDENT LESSON A15 ArrayList

CMSC131. A Simple Problem?

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

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

Introduction to Computer Science I

CS 211: Using ArrayList, Implementing Arraylist

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

Strings and Arrays. Hendrik Speleers

Today's Agenda. > To give a practical introduction to data structures. > To look specifically at Lists, Sets, and Maps

CS 200 Objects and ArrayList Jim Williams, PhD

Fundamental language mechanisms

Algorithms. Produced by. Eamonn de Leastar

Java Collections Framework: Interfaces

Unit5: Packages and abstraction

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

Computer Science II (Spring )

Dynamic Data Structures and Generics

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

CS11 Java. Winter Lecture 8

CITS1001 week 4 Grouping objects

Collections Framework: Part 2

Chapter 10 Collections

Transcription:

Computational Expression ArrayList Iterators Janyl Jumadinova 7-14 November, 2018 Janyl Jumadinova Computational Expression 7-14 November, 2018 1 / 11

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 Janyl Jumadinova Computational Expression 7-14 November, 2018 2 / 11

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 Janyl Jumadinova Computational Expression 7-14 November, 2018 2 / 11

Collections Janyl Jumadinova Computational Expression 7-14 November, 2018 3 / 11

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 Janyl Jumadinova Computational Expression 7-14 November, 2018 4 / 11

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 Janyl Jumadinova Computational Expression 7-14 November, 2018 4 / 11

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 Janyl Jumadinova Computational Expression 7-14 November, 2018 4 / 11

A Few ArrayList Methods Janyl Jumadinova Computational Expression 7-14 November, 2018 5 / 11

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. Janyl Jumadinova Computational Expression 7-14 November, 2018 6 / 11

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"); Janyl Jumadinova Computational Expression 7-14 November, 2018 6 / 11

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. Janyl Jumadinova Computational Expression 7-14 November, 2018 7 / 11

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 Janyl Jumadinova Computational Expression 7-14 November, 2018 8 / 11

Iterators Several classes in Java API define iterators Scanner: hasnext(): returns true if there is another input token to process Janyl Jumadinova Computational Expression 7-14 November, 2018 9 / 11

ArrayList of Primitives? The type you specify when creating an ArrayList must be an object type; it cannot be a primitive type. Janyl Jumadinova Computational Expression 7-14 November, 2018 10 / 11

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>(); Janyl Jumadinova Computational Expression 7-14 November, 2018 10 / 11

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>(); Janyl Jumadinova Computational Expression 7-14 November, 2018 10 / 11

Wrapper Classes Example 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); Janyl Jumadinova Computational Expression 7-14 November, 2018 11 / 11