CSE 143 Lecture 2 Collections and ArrayIntList

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
Building Java Programs Chapter 15 Implementing a Collection Class: ArrayIntList Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
Copyright 2008 by Pearson Education Building Java Programs Chapter 10 Lecture 10-1: ArrayList reading: 10.1.
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
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
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 Exceptions and ArrayList slides created by Marty Stepp
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 10 ArrayList Copyright (c) Pearson All rights reserved.
Using the Java Collection Libraries COMP 103 # T2
slides created by Marty Stepp
Collections.
Building Java Programs
slides created by Marty Stepp
Building Java Programs Chapter 14
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Welcome to CSE 143!.
Building Java Programs
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Building Java Programs
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
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Building Java Programs
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Chapter 10 ArrayList reading: 10.1
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
Building Java Programs
CSE 143 Lecture 4 More ArrayIntList:
slides created by Ethan Apter
Lecture 13: ArrayLists AP Computer Science Principles
CSE 143 Lecture 4 Implementing ArrayIntList
slides created by Marty Stepp
CSE 143 Lecture 5 References and Linked Nodes
CSE 143 Lecture 3 Implementing ArrayIntList reading:
CSE 143 Lecture 5 More ArrayIntList:
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
slides created by Marty Stepp
Building Java Programs
CSE 143 Lecture 2 ArrayIntList, binary search
slides created by Ethan Apter
slides adapted from Marty Stepp
Building Java Programs
slides created by Marty Stepp
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
slides created by Marty Stepp
CSE 143 Lecture 21 Advanced List Implementation
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Implementing ArrayIntList
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

CSE 143 Lecture 2 Collections and ArrayIntList slides created by Marty Stepp http://www.cs.washington.edu/143/

Collections collection: an object that stores data; a.k.a. a "data structure" the objects stored are called elements some collections maintain an ordering; some allow duplicates typical operations: add, remove, clear, contains (find), get size examples found in the Java class libraries: ArrayList, LinkedList, HashMap, TreeSet, Stack, Queue, PriorityQueue Why should we want to use collections?

Exercise Write a program that reads a file (of unknown size) full of integers and prints the integers in the reverse order to how they occurred in the file. Consider example file data.txt: 17 932085 -32053278 100 3 When run with this file, your program's output would be:

Solution using arrays int[] nums = new int[100]; // make a really big array int size = 0; Scanner input = new Scanner(new File("data.txt")); while (input.hasNextInt()) { nums[size] = input.nextInt(); // read each number size++; // into the array } for (int i = size - 1; i >= 0; i--) { System.out.println(nums[i]); // print reversed index 1 2 3 4 5 6 ... 98 99 value 17 932085 -32053278 100 size

Unfilled arrays We often need to store an unknown number of values. int[] nums = new int[100]; int size = 0; We often need to store an unknown number of values. Arrays can be used for this, but we must count the values. Only the values at indexes [0, size - 1] are relevant. We are using an array to store a list of values. What other operations might we want to run on lists of values? index 1 2 3 4 5 6 ... 98 99 value 17 932085 -32053278 100 size

Other possible operations public static void add(int[] list, int size, int value, int index) public static void remove(int[] list, int size, int index) public static void find(int[] list, int size, int value) public static void print(int[] list, int size) ... We could implement these operations as methods that accept a list array and its size along with other parameters. But since the behavior and data are so closely related, it makes more sense to put them together into an object. A list object can store an array of elements and a size, and can have methods for manipulating the list of elements. Promotes abstraction (hides details of how the list works)

Lists list: a collection storing an ordered sequence of elements, each 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

Exercise Let's write a class that implements a list using an int[] We'll call it ArrayIntList behavior: add(value), add(index, value) get(index), set(index, value) size() remove(index) indexOf(value) ... The list's size will be the number of elements added to it so far How will the list be used?...

Client programs ArrayIntList.java is not, by itself, a runnable program. A class can be used by client programs. ArrayIntList.java (class) public class ArrayIntList { private int[] list; private int size; ... } Main.java (client program) public class Main { public static void main(String[] args) { ArrayIntList list1 = new ArrayIntList(); list1.add(17); list1.add(22); ArrayIntList list2 = new ArrayIntList(); list2.add(-3); list2.add(98); list2.add(2); } index 1 2 ... 9 value 17 22 size index 1 2 ... 9 value -3 98 size 3

Using ArrayIntList construction storing a value retrieving a value int[] numbers = new int[5]; ArrayIntList list = new ArrayIntList(); storing a value retrieving a value numbers[0] = 42; int n = numbers[0]; list.add(42); int n = list.get(0); searching for the value 27 for (int i = 0; i < numbers.length; i++) { if (numbers[i] == 27) { ... } } if (list.indexOf(27) >= 0) { ... }

Pros/cons of ArrayIntList pro (benefits) simple syntax don't have to keep track of array size and capacity has powerful methods (indexOf, add, remove, toString) con (drawbacks) ArrayIntList only works for ints (arrays can be any type) syntax is different to learn and use

Implementing add Add to end of list is easy; just store element and increase size public void add(int index, int value) { list[size] = value; size++; } list.add(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 12 42 size

Implementing add (2) Adding to the middle or front is hard (see book ch 7.3) must shift nearby elements to make room for the new value list.add(3, 42); 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

Implementing add (2) list.add(3, 42); public void add(int index, int value) { for (int i = size; i > index; i--) { list[i] = list[i - 1]; } list[index] = value; 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

Implementing remove list.remove(2); 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); 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

Preconditions What happens if the client tries to access an element that is past the size but within the bounds of the array? Example: list.get(11); on a list of 5 elements, capacity 100 We have not addressed this case yet, and currently we just choose to assume that the user will not do such a thing. 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 void remove(int index) { return elementData[index]; }