CS 211: Using ArrayList, Implementing Arraylist

Similar documents
CS 211: Using ArrayList, Implementing Arraylist

CS 310: ArrayList Implementation

CS 310: HW 1, Junit, Generics Review

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

CS Programming I: ArrayList

CS 302: Introduction to Programming in Java. Lecture 12

Arrays and ArrayLists. David Greenstein Monta Vista High School

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

Lecture 6: ArrayList Implementation

CS 310: Array- and Linked-based Data Structures

Computer Science 210 Data Structures Siena College Fall 2018

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

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

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

Working with arrays. ArrayLists. Abstraction. Arrays

Computational Expression

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

ArrayLists. COMP1400 Week 8. Wednesday, 12 September 12

CITS1001 week 4 Grouping objects

Introduction to Computer Science I

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

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

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

CS 211: Methods, Memory, Equality

AP Computer Science Lists The Array type

Garbage Collection (1)

ArrayLists. Chapter 12.1 in Savitch

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional)

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1

Building Java Programs

Using arrays to store data

1.00/ Lecture 8. Arrays-1

CS 106A, Lecture 19 ArrayLists

Review for Test 1 (Chapter 1-5)

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

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

AP Computer Science. ArrayLists

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

What are Generics? e.g. Generics, Generic Programming, Generic Types, Generic Methods

Introduction to Programming Using Java (98-388)

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

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

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

CS 200 Objects and ArrayList Jim Williams, PhD

CSC 222: Object-Oriented Programming. Fall 2017

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

Java Primer 1: Types, Classes and Operators

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

CS 310: Order Notation (aka Big-O and friends)

CS108, Stanford Handout #8. Java Generics

CS 106A, Lecture 27 Final Exam Review 1

Review: Array Initializer Lists

Interfaces, collections and comparisons

CSE 8B Intro to CS: Java

CS 211: Recursion. Chris Kauffman. Week 13-1

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

CMSC 132: Object-Oriented Programming II

Examination Questions Midterm 1

Computer Science II (Spring )

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CSC 204 Lab 12: Intro to ArrayLists

Addendum File Web Design and Development (CS506)

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

CMSC 132: Object-Oriented Programming II. Generic Programming. Department of Computer Science University of Maryland, College Park

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

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

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

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

CS 222: Pointers and Manual Memory Management

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CS 211: Existing Classes in the Java Library

CSIS 10B Lab 2 Bags and Stacks

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CS 106A, Lecture 20 ArrayLists and HashMaps

Computer Science II Lecture 2 Strings, Vectors and Recursion

Big O & ArrayList Fall 2018 Margaret Reid-Miller

1.1. Annotations History Lesson - C/C++

Algorithms. Algorithms 1.3 STACKS AND QUEUES. stacks resizing arrays queues generics iterators applications ROBERT SEDGEWICK KEVIN WAYNE.

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

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

Generic classes & the Java Collections Framework. *Really* Reusable Code

Parsing JSON, Using Libraries, Java Collections, Generics

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Inheritance. Improving Structure with Inheritance. Dr. Siobhán Drohan Mairead Meagher. Produced by:

The list abstract data type defined a number of operations that all list-like objects ought to implement:

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

Lists using LinkedList

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

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

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

Programming Basics. Digital Urban Visualization. People as Flows. ia

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

CSE 331. Generics (Parametric Polymorphism)

Transcription:

CS 211: Using ArrayList, Implementing Arraylist Chris Kauffman Week 12-1

Collections Java has a nice library of containers, Collections framework Interfaces that provide get, set, add methds, conversion to arrays All have parameterized types: ArrayList<E> We ll mostly be interested in ArrayList Like arrays but lacking nice [ ] syntax Use get and set instead Later in your studies TreeSet<E>, TreeMap<K,V>, HashSet<E>, HashMap<K,V>

ArrayList Crash Course ArrayList is an array that can grow at runtime with add(x) Can hold any kind of type like arrays New syntax with angle braces at work: ArrayList<String> as = new ArrayList<String>(); as.add("hi"); as.add("bye"); System.out.println(as.get(1)); Have a look at UseArrayList.java

ArrayList Goodies JavaDoc for ArrayList a.get(5) a.set(5, x) a.add(x) a.add(i,x) int n = a.size(); int i = a.indexof(x) access assignment append, grow if needed insert, shift/grow as needed how many elements linear search Big win in ArrayList over standard arrays: they grow as needed How could that work? You should want to know...

Reminder: No Primitives Allowed Can t do ArrayList<int> a =... No primitives allowed; Instead do ArrayList<Integer> a =... Boxed and Unboxed Boxed Integer Double Character Float Boolean Unboxed int double char float boolean Compiler is smart about converting between these two

Collection Classes, Collections Methods ArrayLists are a Collection Part of Java s collections framework Implements interface Collection<E> JavaDoc for Collection interface, basic access/assignment/size methods Doing Stuff to Collections Many things one wants to do to a Collection sort binaryseach max/min swap addall The Collections (notice the trailing "s") has a lot of static methods to do the above operations to any class implementing Collection JavaDoc for Collections class These all look weird, mention a Comparator, we ll get to that soon

Exercise: Naive Median Calculation Median Age File stores name/age pairs Compute the median of the ages Median is the middle score of the sorted ages Advice Use ArrayList to make input easy Use a Collections method to make sorting easy Use appropriate ArrayList methods to access elements Use Integer rather than int Input File names-ages.txt Dexter 35 Debra 32 Angelos 43 Vincent 30 Maria 39 James 39 Brian 37 Harrison 1 Rita 29 Cody 9 Lila 28 Demo Run > javac ComputeMedian.j > java ComputeMedian na Sorted ages: 0: 1 1: 9 2: 28 3: 29 4: 30 5: 32 6: 35 7: 37 8: 39 9: 39 10: 43 median: 32

Saving Code Space Can save a little space by eliding LHS type param in assignments ArrayList<Pair<Integer>> api = new ArrayList<Pair<Integer>>(); Instead do.. ArrayList api = new ArrayList<Pair<Integer>>(); but later if you do Integer i = api.get(0); expect compiler warnings. The following line will get you something interesting ArrayList<Integer> a = new ArrayList();

Type Inference for Space Saving Java can do a limited amount of type inference with generics Automatically match type of left-hand side to right-hand side of assignment Example ArrayList<String> as = new ArrayList<>(); The empty angle brackets in ArrayList<>(); ask the compiler to infer the type based on the context

You Might Very Well See When working with generics, may get compile warnings Note: TypeWarnings.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Recompile with -Xlint:unchecked javac -Xlint:unchecked TypeWarnings.java TypeWarnings.java:3: warning: [unchecked] unchecked conversion found : java.util.arraylist required: java.util.arraylist<java.lang.integer> ArrayList<Integer> a = new ArrayList(); What s up?

ArrayList Implementation Q: How would you build ArrayList? Have generics <T> and used ArrayList Try to recreate some parts How expensive are operations like get(), set(), add()? Will continue this kind of discussion in CS 310 Todays Code Includes.. Moderatly complete version: MiniAL.java (76 lines) java.util.arraylist source code (1172 lines)

MiniAL: Simplified ArrayList Functionality Generic so contains any type A wrapper around an array: data Two Notions of Size for MiniAL Buffer size: data.length Virtual size: number a.add(x) calls Keep a field size of add() calls v.size() returns virtual size v.get(i) and v.set(i,x) map directly to array ops v.add(x) may require expand/copy of underlying data array Reading Examine MyVector.java All of BJP Ch 15 builds up an ArrayList equivalent