Implements the List Interface  What is an interface?  all abstract methods  can not have instance variables  List is an interface  ArrayList implements.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Java Review Interface, Casting, Generics, Iterator.
Hip Hip Array! AP Computer Science. Remember Strings? Strings are an array of characters An array is a collection of variables all of the same type. Arrays.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
List Interface CS java.util.List Interface and its Implementers CS340 2.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Multiple Choice Solutions True/False a c b e d   T F.
1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
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.
Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
ArrayLists (and the for-each loop) ArrayList example = new ArrayList (); example.add(4);
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 ArrayLists Section 9.9.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
CSE 1201 Object Oriented Programming ArrayList 1.
SSS 2.1 – Huntsville 2/22/14 Carol Yarbrough Alabama School of Fine Arts Arrays.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
CMSC 202 ArrayList Aug 9, 2007.
Sixth Lecture ArrayList Abstract Class and Interface
Lecture 20: Wrapper Classes and More Loops
Single-Linked Lists.
(like an array on steroids)
ARRAYLIST AND VECTOR.
COP 3503 FALL 2012 Shayan Javed Lecture 8
TCSS 143, Autumn 2004 Lecture Notes
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Data Type (how to view it)
Building Java Programs
CMSC 202 ArrayList Aug 9, 2007.
ArrayList Collections.
Lecture 2: Implementing ArrayIntList reading:
CMSC 202 ArrayList Aug 9, 2007.
Grouped Data Arrays, and Array Lists.
Code Refresher Test #1 Topics:
ArrayLists 22-Feb-19.
Collections Framework
slides created by Marty Stepp
CSE 143 Lecture 3 Implementing ArrayIntList reading:
ArrayLists.
ArrayLists 27-Apr-19.
CSE 143 Lecture 4 Implementing ArrayIntList reading:
Presentation transcript:

Implements the List Interface  What is an interface?  all abstract methods  can not have instance variables  List is an interface  ArrayList implements List  What other interface have you seen?  Grid

ArrayList MethodsArrayList Methods  int size()  boolean add(E obj)  void add(int index, E obj)  E get(int index)  E set(int index, E obj)  E remove(int index)  void clear()

What can ArrayList hold?What can ArrayList hold?  Object references  No primitives!  Primitives are Auto-Boxed - converted to corresponding class (int to Integer, etc.)

Advantages / Disadvantages  Advantages  Easier to manipulate  Methods available to insert, delete, access  Easily /automatically resized  Disadvantages  One dimensional only  Can have ArrayLists of ArrayLists instead  Less efficient – space and time

Define import java.util.ArrayList; public class arrayListExample { private ArrayList B ; public arrayListExample() { B = new ArrayList (); B.add("tomato"); B.add("pear"); B.add("durian"); B.add("apple"); B.add("mango"); }

Populating private ArrayList card; card.add(0,55); // add 55 before 0 index card.add(99); // add 99 at end of list Finding Length int clen = card.size();

Accessing elements in a loop for ( int i = 0; i < stringList.size(); i++) { String item = stringList.get(i); System.out.println("Item " + i + " : " + item); } for ( String item: stringList){ System.out.println("retrieved element: " + item); }

ArrayList intList = new ArrayList (); int n = 1; boolean addMore = true; while (addMore) { intList.add(10 * n); n++; if (n == 100) addMore = false; }

For Each LoopFor Each Loop  Can’t be used to change an ArrayList object! List list = new ArrayList (); list.add("Hi"); … for (String s : list) s = "NO"; // does not change list objects

For Each LoopFor Each Loop  Can be used to mutate a referenced object! List bugs = new ArrayList (); bugs.add(new Bug()); … for (Bug b : bugs) b.setColor(Color.GREEN);// bugs are now green Can’t add or remove inside For-Each loop! Gets ConcurrentModificationException.

Generic TypesGeneric Types  Generic Types form a hierarchy (is-a).  Hierarchy is based on the base type.  Hierarchy is not based on the parameters to that type.  ArrayList is-a List  ArrayList is-not-a List

 ArrayList actors1 = new ArrayList (); // Good  ArrayList actors2 = new ArrayList (); // BAD  List actors3 = new ArrayList (); // Good  List actors4 = actors1;// BAD

Comparing List ElementsComparing List Elements ArrayList myList = new ArrayList (); // elements added to myList for (int idx=0; idx < myList.size(); i++) { if (myList.get(idx).equals(“monkey”) ) System.out.println(“Found a monkey”); }

Comparing List ElementsComparing List Elements ArrayList myList = new ArrayList (); // elements added to myList for (int idx=0; idx < myList.size(); i++) { if (myList.get(idx).compareTo(“monkey”) < 0) System.out.println(“Less than monkey”); if (myList.get(idx).compareTo(“monkey”) == 0) System.out.println(“Equals monkey”); if (myList.get(idx).compareTo(“monkey”) > 0) System.out.println(“More than monkey”); }

public class Student { private String name, school; private int grade; public Student( String nm, int gd, String sch) { //code to set all the instance variables} public String getName() { return name;} public int getGrade() { return grade;} public String getSchool() { return school;} public String toString() { return “Name: ” + getName() + “\n“ + “Grade: “ + getGrade() +“\n” + “School: “ + getSchool() + “\n\n”; }

public class SatSession2Roster { public static void main (String args[]) { ArrayList roster2 = new ArrayList (); }

Multiple Choice QuestionsMultiple Choice Questions

MC AnswersMC Answers  Q1: D // if 2 ones are next to each other the second one gets skipped.  Q2: D  Q3: B // one of the methods was a set() not an add()

Sample Free ResponseSample Free Response

Solution to FR Question PART A: public int getTotalBoxes() { int sum = 0; for (CookieOrder co : this.orders) { sum += co.getNumBoxes(); } return sum; }

PART B:PART B: public int removeVariety(String cookieVar) { int numBoxesRemoved = 0; for (int i = this.orders.size() - 1; i >= 0; i--) { if (cookieVar.equals(this.orders.get(i).getVariety())) { numBoxesRemoved += this.orders.get(i).getNumBoxes(); this.orders.remove(i); } return numBoxesRemoved; }

Alternative solution (forward traversal direction): public int removeVariety(String cookieVar) { int numBoxesRemoved = 0; int i = 0; while (i < this.orders.size()) { if (cookieVar.equals(this.orders.get(i).getVariety()) { numBoxesRemoved += this.orders.get(i).getNumBoxes(); this.orders.remove(i); } else i++; } return numBoxesRemoved; }