04-arraylist - CSE143 Lecture4 ExceptionsandArrayList

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 143 Lecture 4 Exceptions and  ArrayList slides created by Marty Stepp http://www.cs.washington.edu/143/
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Preconditions precondition : Something your method  assumes is true at the start of its execution. Often documented as a comment on the method's header: // Returns the element at the given index. // Precondition: 0 <= index < size public int get(int index) { return elementData[index]; } Stating a precondition doesn't really "solve" the problem Clients don’t always follow directions. Precondition is non-binding What if we want to actually enforce the precondition?
Background image of page 2
3 Bad precondition test What is wrong with the following way to handle violations? // Returns the element at the given index. // Precondition: 0 <= index < size public int get(int index) { if (index < 0 || index >= size) { System.out.println("Bad index! " + index); return -1; } return elementData[index]; } -1 could be a legal value println  is not a very strong deterrent to the client  (esp. GUI)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Throwing exceptions (4.4) throw new ExceptionType (); throw new ExceptionType (" message "); Causes the program to immediately crash with an exception. Common exception types: ArithmeticException, ArrayIndexOutOfBoundsException, FileNotFoundException,  IllegalArgumentException, IllegalStateException, IndexOutOfBoundsException,  IOException, NoSuchElementException, NullPointerException, RuntimeException,  UnsupportedOperationException Why would anyone ever  want   the program to crash?
Background image of page 4
5 Exception example public int get(int index) { if (index < 0 || index >= size) { String msg = "Out of list bounds: " + index; throw new IndexOutOfBoundsException (msg); } return elementData[index]; } Exercise: Modify the rest of  ArrayIntList  to state  preconditions and throw exceptions as appropriate.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 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.*;
Background image of page 6
7 Java collection framework
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Lists list : a collection storing an ordered sequence of elements each element is accessible by a 0-based  index a list has a  size  (number of elements that have been added) elements can be added to the front, back, or elsewhere
Background image of page 8
9 Idea of a list Rather than creating an array of boxes, create an object that  represents a "list" of items.  (initially an empty list.) [] You can add items to the list. The default behavior is to add to the end of the list.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 29

04-arraylist - CSE143 Lecture4 ExceptionsandArrayList

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online