Lesson 26: ArrayList (W08D1)

Similar documents
Lesson 36: for() Loops (W11D1)

Lesson 24: Recursive Algorithms #1 (W07D3)

CITS1001 week 4 Grouping objects

Lesson 10: Quiz #1 and Getting User Input (W03D2)

Methods. Every Java application must have a main method.

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

Lab Activity Plan. John Dalbey CPE /30/2013

Introduction to Computer Science I

Lesson 12: OOP #2, Accessor Methods (W03D4)

Lesson 39: Conditionals #3 (W11D4)

APCS Semester #1 Final Exam Practice Problems

Introduction to Generics in Java 5

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

ArrayLists. Chapter 12.1 in Savitch

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

Lesson 38: Conditionals #2 (W11D3)

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

Computer Science 210 Data Structures Siena College Fall 2018

CS 231 Data Structures and Algorithms Fall 2018

CS1083 Week 2: Arrays, ArrayList

Using arrays to store data

CS Programming I: ArrayList

CMSC 132: Object-Oriented Programming II

Principles of Software Construction: Objects, Design, and Concurrency

Computational Expression

Big O & ArrayList Fall 2018 Margaret Reid-Miller

AP CS Unit 7: Interfaces. Programs

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

Lesson 04: Our First Java Program (W01D4

Examination Questions Midterm 1

CS 302: Introduction to Programming in Java. Lecture 12

Lesson 2.4 Arraylist

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

Lesson 43.. ArrayList

Introduction to Java Programming

University of Palestine. Mid Exam Total Grade: 100

AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s).

AP CS Unit 7: Interfaces Exercises Assume all code compiles unless otherwise suggested.

Working with arrays. ArrayLists. Abstraction. Arrays

Java generics. h"p:// h"p://

ArrayLists. COMP1400 Week 8. Wednesday, 12 September 12

Generics and Type Safety. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CSC 204 Lab 12: Intro to ArrayLists

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Grouping Objects (I)

Arrays. Eng. Mohammed Abdualal

Slides are adapted from the originals available at

Intro to Computer Science II

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

ArrayList, Hashtables, Exceptions, and Files

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

Administrivia. Last modified: Fri Aug 25 10:59: CS61B: Lecture #2 1

AP Computer Science Homework Set 1 Fundamentals

Array Lists. CSE 1310 Introduction to Computers and Programming University of Texas at Arlington. Last modified: 4/17/18

Review: Array Initializer Lists

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

AP Programming - Chapter 13 Lecture page 1 of 17

Csci 102: Sample Exam

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

PIC 20A Number, Autoboxing, and Unboxing

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

Java Collections Framework: Interfaces

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

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

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

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

First Name Last Name ID#

Loops. CSE 114, Computer Science 1 Stony Brook University

1.00/ Lecture 8. Arrays-1

Some Sample AP Computer Science A Questions - Solutions

Introduction to Java Unit 1. Using BlueJ to Write Programs

CompSci 125 Lecture 09. Chapter 5: while, break and continue statements Iterators and the ArrayList Class

CP122 CS I. Iteration

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

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

public static void main(string args[]){ arraylist(); recursion();

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

CSE 143 Lecture 4. Preconditions

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

Dynamically sized arrays

Java generics. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

MODULE 6q - Exceptions

Warm up question. 2)Which of the following operators are defined on a half integer? Which of the following would it be useful to write as behaviors?

Final Examination Semester 2 / Year 2011

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

1) Consider the following code segment, applied to list, an ArrayList of Integer values.

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

AP Computer Science Unit 1. Writing Programs Using BlueJ

Lecture 5: Methods CS2301

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

St. Edmund Preparatory High School Brooklyn, NY

JAVA OPERATORS GENERAL

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

AP Computer Science Unit 1. Writing Programs Using BlueJ

Object Oriented Programming. Java-Lecture 6 - Arrays

Transcription:

Lesson 26: ArrayList (W08D1) Balboa High School Michael Ferraro October 5, 2015 1 / 25

Do Now Prepare PS #4a (paper form) for pick-up! Consider the code below for powiter(), an iterative algorithm that raises a base, a, to the specified power, b. E.g., powiter(2,3) computes 2 3, returning 8. public static int powiter(int a, int b) { int result = a; while (??? ) { result *= a; b--; return result; Using a paper table, determine the stopping condition to use for the while() loop. Afterward, verify using Java. 2 / 25

Aim Students will learn how ArrayLists work, and how iterative and recursive procedures may take advantage of them. 3 / 25

What s a list? Run this Snap! program: save this file: list adder.xml import into Snap! Then answer the following: What does a list appear to be? What is the purpose of i? 4 / 25

Iterating through a List 5 / 25

What is an ArrayList? An ArrayList is a Java object that can hold a set (or list) of objects that may be: added removed iterated through... and more (later) 6 / 25

What is an ArrayList? An ArrayList is a Java object that can hold a set (or list) of objects that may be: added removed iterated through... and more (later) It s a Java class, complete with an API here 7 / 25

What is an ArrayList? An ArrayList is a Java object that can hold a set (or list) of objects that may be: added removed iterated through... and more (later) It s a Java class, complete with an API here ArrayList is part of the java.util package, so when you use this class, you need to have this import statement: import java.util.arraylist; 8 / 25

Our first ArrayList Let s create an ArrayList to hold String objects. import java.util.arraylist; public class ArrayListOne { //create file in Lesson26 project! public static void main(string[] args) { ArrayList<String> lista = new ArrayList<String>(); 9 / 25

Our first ArrayList Let s create an ArrayList to hold String objects. import java.util.arraylist; public class ArrayListOne { //create file in Lesson26 project! public static void main(string[] args) { ArrayList<String> lista = new ArrayList<String>(); String onestr = "one"; lista.add(onestr); 10 / 25

Our first ArrayList Let s create an ArrayList to hold String objects. import java.util.arraylist; public class ArrayListOne { //create file in Lesson26 project! public static void main(string[] args) { ArrayList<String> lista = new ArrayList<String>(); String onestr = "one"; lista.add(onestr); lista.add("two"); lista.add("three"); 11 / 25

Our first ArrayList Let s create an ArrayList to hold String objects. import java.util.arraylist; public class ArrayListOne { //create file in Lesson26 project! public static void main(string[] args) { ArrayList<String> lista = new ArrayList<String>(); String onestr = "one"; lista.add(onestr); //adding a NAMED String lista.add("two"); //adding an ANONYMOUS String lista.add("three"); 12 / 25

Our first ArrayList Let s create an ArrayList to hold String objects. import java.util.arraylist; public class ArrayListOne { //create file in Lesson26 project! public static void main(string[] args) { ArrayList<String> lista = new ArrayList<String>(); String onestr = "one"; lista.add(onestr); lista.add("two"); lista.add("three"); System.out.println(listA); //what *method* must //ArrayList provide? 13 / 25

Let s Iterate through lista Comment out System.out.println() and replace with: for ( String curstr : lista ) { System.out.println("current element:\t" + curstr); 14 / 25

Let s Iterate through lista Comment out System.out.println() and replace with: for ( String curstr : lista ) { System.out.println("current element:\t" + curstr); This form of a for() loop is a for-each loop: for each String in lista, do... 15 / 25

Let s Iterate through lista Comment out System.out.println() and replace with: for ( String curstr : lista ) { System.out.println("current element:\t" + curstr); This form of a for() loop is a for-each loop: for each String in lista, do... Iterating through an ArrayList this way does not modify the object; this is simply a way to inspect the contents of the ArrayList. 16 / 25

Let s Add Integers We are now going to iterate through a list of Integers, returning the sum. 1 This is due to a relatively new Java feature called autoboxing that we might discuss later. 17 / 25

Let s Add Integers We are now going to iterate through a list of Integers, returning the sum. ArrayLists hold objects, not primitive variables like ints; that s OK since there s a Java class called Integer, which will work the same for us 1 1 This is due to a relatively new Java feature called autoboxing that we might discuss later. 18 / 25

Let s Add Integers We are now going to iterate through a list of Integers, returning the sum. ArrayLists hold objects, not primitive variables like ints; that s OK since there s a Java class called Integer, which will work the same for us 1 Download ArrayListTwo.java from here and import into Lesson26 Read the code for the program, ask questions about what you don t understand 1 This is due to a relatively new Java feature called autoboxing that we might discuss later. 19 / 25

Two-Minute Exercise Quick: Go to the API for ArrayList here and find a method that will return the length of a list. Modify ArrayListTwo to print the length of the list. 20 / 25

And in Strolls Recursion... How might you think of the adding problem recursively? 21 / 25

And in Strolls Recursion... How might you think of the adding problem recursively? The sum of the list = first element + the sum of the rest of the elements 22 / 25

And in Strolls Recursion... How might you think of the adding problem recursively? The sum of the list = first element + the sum of the rest of the elements We need a way to get the first element off of a list: remove() method 2 ArrayList<Integer> mylist = new ArrayList<Integer>(); mylist.add(6); mylist.add(7); mylist.add(8); System.out.println(myList); int firstelt = mylist.remove(0); //0th elt is the first one System.out.println("sum = " + firstelt + " + " sumof" + mylist); 2 NOTE: remove() modifies the ArrayList object forever! 23 / 25

Classwork You ve been given most of the basics of ArrayList that you need for PS #4b 5.1 & 5.4 of PS #4b should already be done (or in progress) Finish 5.2 5.3, inclusive, of PS #4b Already done with those sections? Write a recursive procedure to take an ArrayList of one-character Strings and return it in reversed form as a single String For example, take an ArrayList like this one: a b c d e f and return fedcba 24 / 25

HW Finish all of 5 of PS #4b. 25 / 25