04-arrayintlist - CSE 143 Lecture 4 Implementing...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: CSE 143 Lecture 4 Implementing ArrayIntList reading: 15.1 - 15.3 slides created by Marty Stepp http://www.cs.washington.edu/143/ 2 Exercise Pretend for a moment that there is no ArrayList class . Write a program that reads a file data.txt (of unknown size) full of integers and prints them in reverse order. 17 932085-32053278 100 3 Output: 3 100-32053278 932085 17 3 "Unfilled array" solution We are using an array to store a l i s t of values. Only the values at indexes [0, s i z e - 1] are relevant. int nums = new int[100]; // make a 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 } 5 s i z e i n d e x 1 2 3 4 5 6 . . . 9 8 9 9 v a l u e 17 932085 -32053278 100 3 ... 4 Possible list 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 write methods that accept a l i s t array and its s i z e . But since this data and this behavior 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. It can also have methods for manipulating the list of elements. This will give us abstraction (hide the details of how the list works) 5 Exercise Let's write a class that implements a list using an int We'll call it ArrayIntList its behavior: add( value ) , add( index , value ), get( index ) , set( index , value ), size(), isEmpty(), remove( index ), indexOf( value ), contains( value ), toString(), ......
View Full Document

Page1 / 22

04-arrayintlist - CSE 143 Lecture 4 Implementing...

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

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