02-arrayintlist

# 02-arrayintlist - 1 CSE 143 Lecture 2 ArrayIntList binary...

This preview shows pages 1–6. Sign up to view the full content.

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

View Full Document

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CSE 143 Lecture 2 ArrayIntList , binary search slides created by Ethan Apter and Marty Stepp http://www.cs.washington.edu/143/ 2 Lists • Lists can be found everywhere – Shopping list – Top 10 list – List of students – List of items Mario has collected – List of types of lists (like this one) • To keep things simple, we’ll build a list of integers. – What is needed? • Something to keep track of the contents of the list (array) • Something to keep track of how many things are in the list (int) 2 3 Lists • We could write something like this: public static void main(String args) { int list = new int[10]; int size = 0; // add 10, -4, 23 to list list[0] = 10; list[1] = -4; list[2] = 23; size = 3; for (int i = 0; i < size; i++) { System.out.println("Element " + i + " is: " + list[i]); } } • What is wrong with this list? – List code not reusable (every person who wants a list must create their own array and manage it manually) – List code prone to error (what if I set the size to 4 instead of 3?) 4 Lists • We’re going to make the notion of a list into a class (the blueprint for list objects). • Each list object will contain an array and the list size. • Each list object will protect (encapsulate) the data so that you can’t “break” the list (e.g., can’t change the size arbitrarily) • Each list object will have a nice, friendly interface. 3 5 ArrayIntList Client Code • First we need some client code to test our new list class: public class ArrayIntListClient { public static void main(String args) { // construct ArrayIntList list1 // construct ArrayIntList list2 // add 6, 43, 97 to list1 // add 72, -8 to list2 // print list1 // print list2 } } We need to make an ArrayIntList class to construct 6 ArrayIntList • What variables should be in ArrayIntList ? – an array, to store the int s – an int , to store the size • The size variable allows us to distinguish between our list’s capacity (the amount the array can hold) and our list’s size (the amount of valid data in the array) • Variable code int elementData = new int[100]; int size = 0; 4 7 ArrayIntList • We don’t want to force the client to make these two variables • Instead, we’ll encapsulate the variables in an object public class ArrayIntList { int elementData = new int[100]; int size = 0; } 8 ArrayIntList Client Code • Now we can update our client code to construct ArrayIntList s: public class ArrayIntListClient { public static void main(String args) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); // add 6, 43, 97 to list1 // add 72, -8 to list2 // print list1 // print list2 } } We need to add values to the ArrayIntList Updated 5 9 ArrayIntList Client Code: Attempt • We might try something like this: public class ArrayIntListClient { public static void main(String args) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); // add 6, 43, 97 to list1 list1.elementData[0] = 6; list1....
View Full Document

## This note was uploaded on 09/29/2010 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

### Page1 / 16

02-arrayintlist - 1 CSE 143 Lecture 2 ArrayIntList binary...

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

View Full Document
Ask a homework question - tutors are online