Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.

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

Generics and the ArrayList Class
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
1 Class Design CS 3331 Fall Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode method.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
Java Syntax Primitive data types Operators Control statements.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Chapter 3 Introduction to Collections – Stacks Modified
04/29/ Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Effective Java: Generics Last Updated: Spring 2009.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
Chapter 8: Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
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.
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Java Type System and Object Model Horstmann ch , 7.7.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSE 1201 Object Oriented Programming ArrayList 1.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Collections Dwight Deugo Nesa Matic
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Building Java Programs Generics, hashing reading: 18.1.
The hashCode method.
Lecture 2: Implementing ArrayIntList reading:
null, true, and false are also reserved.
Programming in Java Lecture 11: ArrayList
ArrayLists.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Object Oriented Programming in java
Generic Classes and Methods
Grouped Data Arrays, and Array Lists.
ArrayLists 22-Feb-19.
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
List Implementation via Arrays
Recursive Objects Singly Linked Lists.
Presentation transcript:

Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting on the written test grades  in the last class I will go through a few topics based on your suggestions, hence, if you want a specific topic to be discussed/reviewed, me before Oct 22, 2011  will also discuss the final 1

Arrays [notes Chapter 8a], [AJ 6] 2

What is an Array?  an array is a container object that holds a fixed number of values of a single type.  The length of an array is established when the array is created. After creation, it is fixed. from, 3

How to Use Arrays?  declaring an array int[] anArray; // an array of integersOR int anArray[];  you can use other types: byte, short, long, float, double, boolean, char, String anArray = new int[10]; // create an array of integers anArray[0] = 100; // initialize first element anArray[1] = 200; // initialize second element System.out.println("Element 1 at index 0: "+anArray[0]); System.out.println("Element 2 at index 1: "+anArray[1]); 4

Why use Arrays? 5

Implementing List using Array  the List interface is generic  hence, the type parameter T in List, which captures the type of the elements of the list  to use this interface, the client has to provide a non primitive type as argument  i.e. List represents a list containing Double objects  note that, our List interface is different from the interface java.util.List  for simplicity, it contains fewer methods and some of the methods are specified slightly differently 6

BoundedArrayList Class  in this implementation, the maximum size of the list is bound, later we will see how to make the list grow unboundedly 7

 note that, BoundedArrayList implements the List interface  means, we have to provide an implementation for each method specified in the List interface  the List interface extends the Iterable interface  Means, we have to provide an implementation for the single method iterator specified in the Iterable 8

Constructors public BoundedArrayList(int capacity) { this.size = 0; this.elements = new Object[capacity]; } //default constructor, delegating constructor above public BoundedArrayList() { this(BoundedArrayList.CAPACITY); } 9

The Copy Constructor  we have to create a new array in the copy constructor to copy the content of the original array to the new one public BoundedArrayList(BoundedArrayList list) { this.size = list.size; this.elements = new Object[list.elements.length]; for (int i = 0; i < list.size; i++) { this.elements[i] = list.elements[i]; }  Don’t need to throw an ArrayIndexOutOfBoundException (see the class invariant) 10

 instead of manually creating a new array and copying the content from the old one to the new one, we can delegate to the Arrays class and use the static method copyOf(original, length), which is part of the java.util package public BoundedArrayList(BoundedArrayList list) { this.size = list.size; this.elements = Arrays.copyOf(list.elements, list.elements.length); } 11

getSize Method public int getSize() { return this.size; } 12

get Method  get(index) returns the element of the list with the given index  the return type of the method is T whereas the base type of the array elements is Object  means, we have to cast public T get(int index) throws IndexOutOfBoundsException { if (index >= 0 && index < this.size) { return (T) this.elements[i]; } else { throw new IndexOutOfBoundsException("Invalid index"); } 13

add Method public boolean add(T element) { boolean added; if (this.size == this.elements.length) { added = false; } else { this.elements[this.size] = element; this.size++; added = true; } return added; } 14

contains Method public boolean contains(T element) { boolean found = false; for (int i = 0; i < this.size; i++) { found = found || this.elements[i].equals(element); } return found; }  loop invariant: found == ∃ 0 ≤ j < i : this.elements[j].equals(element) 15

 if we want to exit once we have found the element public boolean contains(T element) { boolean found = false; for (int i = 0; i < this.size && !found; i++) { found = found || this.elements[i].equals(element); } return found; } found = this.elements[i].equals(element); 16

remove Method  loop invariant for the second loop ∀ i f < j < i : this.elements[j] has been copied to this.elements[j − 1] 17

equals Method  loop invariant for the second loop equal == this.size() == other.size() && ∀ 0 j < i : this.elements[j].equals(other.elements[j]) 18

hashCode Method public int hashCode() { final int BASE = 31; int hashCode = 0; for (int i = 0; i < this.size; i++) { hashCode = BASE * hashCode + this.elements[i].hashCode(); } return hashCode; }  loop invariant: hashCode == ∑_i<j<this.size 31 j × this.elements[j].hashCode() 19

hashCode Revised  all class must provide a hashCode() method  it digests the data stored in an instance of the class into a single hash value (identifier) and used by other code when storing or manipulating the instance  we must override hashCode() in a way so that it behaves in a way consistent with the object's equals() method  that is, an object must report the same hash value when equals() returns true.  the implementation of hashCode() may differ, which means, two unequal objects having different hashes is desirable but not mandatory 20

 in a nutshell:  if x.equals(y) returns true then x.hashCode() and y.hashCode() return the same integer for all x and y different from null  the opposite is not required, but may improve the performance of methods in some classes 21

 we must override hashCode() in a way so that it behaves in a way consistent with the object's equals() method  that is, an object must report the same hash value when equals() returns true.  the implementation of hashCode() may differ, which means, two unequal objects having different hashes is desirable but not mandatory 22

toString Method public String toString() { StringBuffer representation = new StringBuffer("["); if (this.size != 0) { for (int i = 0; i < this.size - 1; i++) { representation.append(this.elements[i]); representation.append(", "); } representation.append(this.elements[this.size - 1]); } representation.append("]"); return representation.toString(); } 23

iterator Method public Iterator iterator() { return new BoundedArrayListIterator (this.elements); }  remember, the iterator method in the Iterable interface returns an object of type Iterator  Iterator is an interface (we cannot create instances), hence, we will need to return an instance of a class that implements the Iterator interface  we need to develop a class BoundedArrayListIterator that implements the Iterator interface that will return an instance of the class 24

/* * class invariant: this.next >= 0 && * for all 0 <= i < this.elements.length : this.elements[i] instanceof T */ 25

Unbounded Size public boolean add(T element) { if (this.size == this.elements.length) { this.elements = Array.copyOf(this.elements, this.elements.length * 2); } this.elements[this.size] = element; this.size++; return true; } 26