CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; 8.1 - 8.7 slides created by Marty Stepp and Hélène Martin

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 10
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Building Java Programs
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Building Java Programs
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
Copyright 2008 by Pearson Education Building Java Programs Chapter 10 Lecture 10-1: ArrayList reading: 10.1.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
Copyright 2010 by Pearson Education Building Java Programs Chapter 10 Lecture 21: ArrayList reading: 10.1.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 4 Exceptions and ArrayList slides created by Marty Stepp
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Chapter 3 Implementing Classes
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 10 ArrayList Copyright (c) Pearson All rights reserved.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Using the Java Collection Libraries COMP 103 # T2
slides created by Marty Stepp
Lecture 23:More on Interfaces
Collections.
Collections.
CSE 143 Lecture 3 More ArrayList; object-oriented programming
JAVA COLLECTIONS LIBRARY
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Welcome to CSE 143!.
Building Java Programs Chapter 10
TCSS 143, Autumn 2004 Lecture Notes
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Building Java Programs
CSE 143 Lecture 4 ArrayList Reading: 10.1.
ArrayLists.
Building Java Programs Chapter 10
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Chapter 10 ArrayList reading: 10.1
CSE 143 Lecture 2 Collections and ArrayIntList
Welcome to CSE 143!.
Lecture 1: ArrayList reading: 10.1
Lecture 2: Implementing ArrayIntList reading:
Welcome to CSE 143! Go to pollev.com/cse143.
slides created by Ethan Apter
Lecture 13: ArrayLists AP Computer Science Principles
CSE 143 Lecture 2 More ArrayList; classes and objects
Go to pollev.com/cse143.
slides created by Marty Stepp
Building Java Programs
CSE 143 Lecture 3 More ArrayList; object-oriented programming
slides created by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin

2 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 import java.util.*;

3 Java collection framework

4 ArrayList methods 2 addAll( list ) addAll( index, list ) adds all elements from the given list to this list (at the end of the list, or inserts them at the given index) contains( value ) returns true if given value is found somewhere in this list containsAll( list ) returns true if this list contains every element from given list equals( list ) returns true if given other list contains the same elements iterator() listIterator() returns an object used to examine the contents of the list (seen later) lastIndexOf( value ) returns last index value is found in list (-1 if not found) remove( value ) finds and removes the given value from this list removeAll( list ) removes any elements found in the given list from this list retainAll( list ) removes any elements not found in given list from this list subList( from, to ) returns the sub-portion of the list between indexes from (inclusive) and to (exclusive) toArray() returns the elements in this list as an array

5 Learning about classes The Java API Specification is a huge web page containing documentation about every Java class and its methods.Java API Specification –The link to the API Specs is on the course web site.

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

7 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 grades = new ArrayList (); grades.add(3.2); grades.add(2.7);... double myGrade = grades.get(0); Primitive TypeWrapper Type int Integer double Double char Character boolean Boolean

8 ArrayList "mystery" ArrayList list = new ArrayList (); for (int i = 1; i <= 10; i++) { list.add(10 * i); // [10, 20, 30, 40,..., 100] } What is the output of the following code? for (int i = 0; i < list.size(); i++) { list.remove(i); } System.out.println(list); Answer: [20, 40, 60, 80, 100] –Observation: If the list size or contents are being changed in a loop, that may lead to surprising or incorrect behavior.

9 ArrayList "mystery" 2 ArrayList list = new ArrayList (); for (int i = 1; i <= 5; i++) { list.add(2 * i); // [2, 4, 6, 8, 10] } What is the output of the following code? int size = list.size(); for (int i = 0; i < size; i++) { list.add(i, 42); // add 42 at index i } System.out.println(list); Answer: [42, 42, 42, 42, 42, 2, 4, 6, 8, 10]

10 Exercise Write a method addStars that accepts a list of strings as a parameter and places a * after each element. –Example: if an array list named list initially stores: [the, quick, brown, fox] –Then the call of addStars(list); makes it store: [the, *, quick, *, brown, *, fox, *] // solution public static void addStars(ArrayList list) { for (int i = 0; i < list.size(); i += 2) { list.add(i, "*"); } }

11 Exercise Write a method intersect that accepts two sorted array lists of integers as parameters and returns a new list that contains only the elements that are found in both lists. –Example: if lists named list1 and list2 initially store: –[1, 4, 8, 9, 11, 15, 17, 28, 41, 59] –[4, 7, 11, 17, 19, 20, 23, 28, 37, 59, 81] –Then the call of intersect(list1, list2) returns the list: – [4, 11, 17, 28, 59]

12 Classes and objects class: A program entity that represents: –A complete program or module, or –A template for a type of objects. –( ArrayList is a class that defines a type.) object: An entity that combines state and behavior. –object-oriented programming (OOP): Programs that perform their behavior as interactions between objects. –abstraction: Separation between concepts and details. Objects provide abstraction in programming.

13 Elements of a class public class BankAccount { private String name; // fields: private int id; // data encapsulated private double balance; // inside each object public BankAccount(String name, int id) { this.name = name; // constructor: this.id = id; // initializes this.balance = 0.0; // new objects } public void deposit(double amount) { this.balance += amount; // instance method: } // each object's... // behavior } "implicit parameter": object on which a method was called

14 BankAccount exercise Suppose we have a class BankAccount with the methods: public BankAccount(String name, int id) public void deposit(double amount) public void withdraw(double amount) public double getBalance() public int getID() Make each account keep a log of all its transactions. –Desired: a printLog method that shows all transactions so far. Deposit of $7.82 Withdrawal of $2.55 Deposit of $6.18

15 Objects storing collections An object can have an array, list, or other collection as a field. public class Course { private double[] grades; private ArrayList studentNames; public Course() { grades = new double[4]; studentNames = new ArrayList ();... } Now each object stores a collection of data inside it.