Lecture 2: Implementing ArrayIntList reading:

Slides:



Advertisements
Similar presentations
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Advertisements

CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Building Java Programs
Building Java Programs Chapter 15 Implementing a Collection Class: ArrayIntList Copyright (c) Pearson All rights reserved.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
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 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.
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.
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.
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 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
slides created by Marty Stepp
CS 106X Arrays; Implementing a Collection (ArrayList)
CSE 143 Lecture 3 More ArrayList; object-oriented programming
Building Java Programs
slides created by Marty Stepp
Building Java Programs
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
ArrayLists.
Building Java Programs
Building Java Programs
CSE 143 Lecture 4 ArrayList Reading: 10.1.
slides created by Marty Stepp and Ethan Apter
ArrayLists.
Building Java Programs Chapter 10
slides created by Ethan Apter
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.
Building Java Programs
Lecture 26: Advanced List Implementation
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Chapter 10 ArrayList reading: 10.1
CSE 143 Lecture 2 Collections and ArrayIntList
Lecture 7: Linked List Basics reading: 16.2
Welcome to CSE 143!.
Lecture 1: ArrayList reading: 10.1
Lecture 2: Implementing ArrayIntList reading:
slides created by Ethan Apter
Welcome to CSE 143! Go to pollev.com/cse143.
slides created by Marty Stepp and Hélène Martin
slides created by Ethan Apter
Lecture 13: ArrayLists AP Computer Science Principles
CSE 143 Lecture 4 Implementing ArrayIntList
CSE 143 Lecture 2 More ArrayList; classes and objects
slides created by Marty Stepp
Lecture 16: Arraylist Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Go to pollev.com/cse143.
CSE 143 Lecture 3 Implementing ArrayIntList reading:
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
ArrayLists.
slides created by Marty Stepp
Building Java Programs
slides created by Alyssa Harding
CSE 143 Lecture 2 ArrayIntList, binary search
slides created by Ethan Apter
slides adapted from Marty Stepp
Building Java Programs
CSE 143 Lecture 3 More ArrayList; object-oriented programming
slides created by Marty Stepp
Lecture 7: Linked List Basics reading: 16.2
CSE 143 Lecture 4 Implementing ArrayIntList reading:
slides created by Ethan Apter and Marty Stepp
CSE 143 Lecture 21 Advanced List Implementation
Implementing ArrayIntList
Presentation transcript:

Lecture 2: Implementing ArrayIntList reading: 15.1 - 15.3 CSE 143 Lecture 2: Implementing ArrayIntList reading: 15.1 - 15.3

Wrapper classes Primitive Type Wrapper Type int Integer double Double char Character boolean Boolean 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);

Recall: 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.

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

ArrayList implementation What is an ArrayList's behavior? add, remove, indexOf, etc What is an ArrayList's state? Many elements of the same type For example, unfilled array index 1 2 3 4 5 6 ... 98 99 value 17 932085 -32053278 100 size

ArrayIntList implementation Simpler than ArrayList<E> No generics (only stores ints) Fewer methods: add(value), add(index, value), get(index), set(index, value), size(), isEmpty(), remove(index), indexOf(value), contains(value), toString(), Fields? int[] int to keep track of the number of elements added The default capacity (array length) will be 10

Printing an ArrayIntList Let's add a method that allows clients to print a list's elements. You may be tempted to write a print method: // client code ArrayIntList list = new ArrayIntList(); ... list.print(); Why is this a bad idea? What would be better?

The toString method Tells Java how to convert an object into a String ArrayIntList list = new ArrayIntList(); System.out.println("list is " + list); // ("list is " + list.toString()); Syntax: public String toString() { code that returns a suitable String; } Every class has a toString, even if it isn't in your code. The default is the class's name and a hex (base-16) number: ArrayIntList@9e8c34

toString solution // Returns a String representation of the list. public String toString() { if (size == 0) { return "[]"; } else { String result = "[" + elementData[0]; for (int i = 1; i < size; i++) { result += ", " + elementData[i]; } result += "]"; return result;

Implementing add #2 How do we add to the middle or end of the list? must shift elements to make room for the value (see book 7.4) list.add(3, 42); // insert 42 at index 3 Note: The order in which you traverse the array matters! index 1 2 3 4 5 6 7 8 9 value 12 size index 1 2 3 4 5 6 7 8 9 value 42 12 size

add #2 code public void add(int index, int value) { for (int i = size; i > index; i--) { list[i] = list[i - 1]; } list[index] = value; size++; list.add(3, 42); index 1 2 3 4 5 6 7 8 9 value 12 size index 1 2 3 4 5 6 7 8 9 value 42 12 size

Other methods Let's implement the following methods in our list: get(index) Returns the element value at a given index. set(index, value) Sets the list to store the given value at the given index. size() Returns the number of elements in the list. isEmpty() Returns true if the list contains no elements; else false. (Why write this if we already have the size method?)

Implementing remove Again, we need to shift elements in the array this time, it's a left-shift in what order should we process the elements? what indexes should we process? list.remove(2); // delete 9 from index 2 index 1 2 3 4 5 6 7 8 9 value 12 size index 1 2 3 4 5 6 7 8 9 value 12 size

Implementing remove code public void remove(int index) { for (int i = index; i < size; i++) { list[i] = list[i + 1]; } size--; list[size] = 0; // optional (why?) list.remove(2); // delete 9 from index 2 index 1 2 3 4 5 6 7 8 9 value 12 size index 1 2 3 4 5 6 7 8 9 value 12 size