02-arrayintlist - CSE143 Lecture2 ArrayIntList,binarysearch

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

View Full Document Right Arrow Icon
CSE 143 Lecture 2 ArrayIntList , binary search slides created by Ethan Apter and 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 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)
Background image of page 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?)
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 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.
Background image of page 4
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
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 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;
Background image of page 6
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; }
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 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
Background image of page 8
9 ArrayIntList  Client Code: Attempt We might try something like this: public class ArrayIntListClient { public static void main(String[] args) { ArrayIntList list1 = new ArrayIntList();
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 / 31

02-arrayintlist - CSE143 Lecture2 ArrayIntList,binarysearch

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