Download presentation source

Download Report

Transcript Download presentation source

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
1
Review
•A declaration like int [ ] g; results in a box for g that
contains the value null:
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
0
1
2
3
4
…
n-1
•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]).
CS100A, Lecture 12, 8
October 1998
2
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
3
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 changing 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
4
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
5
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
6
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
3
maxno
300
items
0
1
2
3
4
…
204 299
“G” “A” “B”
CS100A, Lecture 12, 8
October 1998
7
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;
}
3
no
0
1
2
3
4
…
204 299
items “G” “A” “B”
CS100A, Lecture 12, 8
October 1998
8
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
“A”
no
5
0
1
2
3
4
…
204 299
items “G” “A” “B” “X” “Y”
CS100A, Lecture 12, 8
October 1998
9
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
d n
? n
#
p
q
add String n to the list
delete String n from the list
report whether n is in the list
print the size of the list
print the list
quit */
CS100A, Lecture 12, 8
October 1998
10