CS100A Lect. 12, 8 Oct More About Arrays

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
1 Project 7: Northwind Traders Order Entry. 2 Northwind Order Entry Extend the Select Customer program from Project 6 to permit the user to enter orders.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Unit Testing CSIS 3701: Advanced Object Oriented Programming.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Arrays Chapter 7.
Chapter VII: Arrays.
The need for Programming Languages
Lecture 5 of Computer Science II
Introduction to Arrays
GC211 Data Structure Lecture 1 Sara Alhajjam.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Chapter 5 Ordered List.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 Part 1 Edited by JJ Shepherd
Stacks.
(Tuesday, 20 October, 7:30-9PM) Encryption-Decryption
JavaScript: Functions.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 10: An Array Instance Variable
The University of Texas Rio Grande Valley
The relational operators
Chapter 5 Ordered List.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Building Java Programs
© Akhilesh Bajaj, All rights reserved.
Building Java Programs
MON TUE WED THU
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Chapter 16 Linked Structures
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look UTPA – Fall 2012 This set of slides is revised from lecture.
Sun Mon Tue Wed Thu Fri Sat
Chapter 9: Data Structures: Arrays
CIS 199 Final Review.
Java Programming Language
Data Structures & Algorithms
Introduction to Arrays
CS100J Lecture 13 Previous Lecture Java constructs
Sun Mon Tue Wed Thu Fri Sat
Lists CMSC 202, Version 4/02.
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Sun Mon Tue Wed Thu Fri Sat
Lists CMSC 202, Version 4/02.
CS100A Sections Dec Loop Invariant Review C Review and Example
Review for Midterm 3.
Arrays and ArrayLists.
Online Registration at Stephen F. Austin State University
Structures Chapter 4.
loops revisited I/O arrays
Presentation transcript:

CS100A Lect. 12, 8 Oct. 1998 More About Arrays Review basic rules for arrays Array initializers Develop class List Reading in Holmes for arrays: Chapter 5 (p. 147) CS100A, Lecture 12, 8 October 1998

Review A declaration like int [ ] g; results in a box for g that contains the value null: g Execution of g= new int [n]; where n is any integer expression, allocates an array of n elements and stores a reference to it in g: g.length is the number of elements in array g. The elements of array g are: g[0], g[1], g[2], …, g[g.length-1]. Use g[exp] to refer to an array element, where exp is any intger expression in the range 0..(g.length-1). Here, exp is called the subscript or the index of the array element. It is an error to reference g[exp] if exp is not in the range 0..(g.length-1). Be careful to distinguish the subscript (e.g. 2) from the array element (e.g. g[2]). null 0 1 2 3 4 … n-1 CS100A, Lecture 12, 8 October 1998

Array Initializers int [ ] coins= new int[4]; coins[0]= 1; coins[1]= 5; coins[2]= 10; coins[3]= 25; is equivalent to int [ ] coins= {1, 5, 10, 25}; An array initializer is a list of expressions enclosed in braces. It’s value is a newly allocated array whose elements are initialized with the values in the list. The length of the new array is the number of items in the array initializer. An array initializer can be used only in such a situation. Another example: String days= {“Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun”}; CS100A, Lecture 12, 8 October 1998

Add a new student to the list Check whether a student is in the list New Problem We want to write a program that will maintain a list of names --the names of the people in this class. Things the program will have to do: Add a new student to the list Check whether a student is in the list Delete a student (who has dropped the course) Count how many students are in the class (so that rooms for a prelim can be reserved) Design decision We don’t know what the best way to maintain this list in the program. Also, such a list, with such operations, can be useful in many places. So let’s develop a class that can be used to represent such lists and to perform such operations on them. This: Will allow us to change representations without chang-ing the program that uses the representation. We can play with different ways of storing the data. Will allow us to use the class in other situations. We’ll be able to reuse the code we write. CS100A, Lecture 12, 8 October 1998

Specification of class List (An instance of) class List will contain a list of at most 300 names (Strings). Names are stored in the list in no particular order. No duplicate names appear in the list. Methods desired: Constructor: List( ): An empty list with space for at most 300 Strings Constructor: List(String n): An empty list with space for n Strings. Empty( ): Set the list to empty. add(String n): Add n to the list if it is not already there. delete(String n): Delete n from the list. isIn(String n): Return value of “n is in the list”. size(): Return the number of items in the list. toString ( ): Return a string representation of the list. CS100A, Lecture 12, 8 October 1998

data.add(“Hartmanis”); if (data.isIn(“Conway”) Sample Client Code List data= new List(35); data.add(“Gries”); data.add(“Cardie”); data.add(“Hartmanis”); if (data.isIn(“Conway”) System.out.println(“It’s broken!”); else System.out.println(“All’s well that ends”); data.delete(“Hartmanis”); CS100A, Lecture 12, 8 October 1998

Representation A list items is implemented using an array. An integer variable no will contain the number of entries, and an integer variable maxno will contain the maximum number of items. Definition: Items are in items[0..no-1], and 0 <= no <= maxno and elements of items[0..no-1] are distinct no maxno items 3 300 0 1 2 3 4 … 204 299 “G” “A” “B” CS100A, Lecture 12, 8 October 1998

Illustration of searching for a value // Return index of item n in list --return no if not there private int placeOf (String n) { int i= 0; // invariant: n is not in items[0..i-1] while (i < no && ! equals(n, items[i])) i= i+1; return i; } no items 3 0 1 2 3 4 … 204 299 “G” “A” “B” CS100A, Lecture 12, 8 October 1998

Illustration of delete operation // Delete n from the list (if it is there) public void delete(String n) { int i= placeOf(n); if (i=no) return; // Put items[no-1] in the hole left by items[i] items[i]= items[no-1]; no= no-1; } i n no items “A” 5 0 1 2 3 4 … 204 299 “G” “A” “B” “X” “Y” CS100A, Lecture 12, 8 October 1998

Test program for class List We write a method main that reads in an executes commands (see list of commands given below). This makes it easy to test class List!! We’ll execute this program. This main program as well as class List appears on the course web site. /*The user of the program inputs an integer, the maximum size of the list. Thereafter, the user can input the following commands: a n add String n to the list d n delete String n from the list ? n report whether n is in the list # print the size of the list p print the list q quit */ CS100A, Lecture 12, 8 October 1998