Transcript (ppt)

CS100A, Fall 1997
Lecture 12, Thursday, 9 October
More about Arrays
A List Class
Concepts:
• Review of basic array rules
• Array initializers and the .length member
• Using an array to implement a List class
Reading in Lewis/Loftus
6.1 (208-219)
CS100A, Fall 1997, Lecture 12
1
•
•
•
•
•
Review
When an array is allocated
int[ ] A = new int[size];
size may be any integer expression.
When using A[exp] to refer to an element,
the subscript exp may be any integer
expression.
The elements in an array are numbered
A[0], A[1], …, A[size-1], where size is the
number of elements in the array.
In a reference to an array element A[exp], it
is an error if exp<0 or exp•
size.
Be sure to be careful to distinguish the
subscript (or location) of an array element
from the data that is actually stored in that
location.
CS100A, Fall 1997, Lecture 12
2
Accessing an Array’s length
In Java, arrays are also objects. In addition to
using subscripts to access array elements,
the dot notation can be used to access the
length of an array. Every array has a
constant int member, length, which contains
the number of elements in the array.
Example:
int[ ] A = new int[42];
int k = 0;
// inv: A[0..k-1] = 0
while (k < A.length) {
A[k] = 0;
k = k + 1;
}
// A[0..A.length-1] = 0
CS100A, Fall 1997, Lecture 12
3
Array Initializers
A list of expressions enclosed in braces is
called an array initializer. It’s value is a
newly allocated array whose elements are
initialized with the values given in the list.
The length of the new array equals the
number of items in the initializer list.
Example:
int[ ] coins = {1, 5, 10, 25};
and
int[ ] coins = new int[4];
coins[0] = 1;
coins[1] = 5;
coins[2] = 10;
coins[3] = 25;
are equivalent
CS100A, Fall 1997, Lecture 12
4
New Problem
Create a class List to store a list of integers.
The following operations should be
provided for each List object:
• Constructors: List( ) creates an empty list
with space for 100 integers; List(n) creates
an empty list with space for n integers.
• makeEmpty( ) — set the list to empty.
• add(n) — add n to the list if n is not already
in the list
• delete(n) — delete n from the list
• isMember(n) — yield true if n is in the list,
otherwise false
• size( ) — yield number of items in the list
• toString( ) — yield a string representation
of the list.
Numbers in the list are stored in no
particular order. No duplicate numbers
appear in the list.
CS100A, Fall 1997, Lecture 12
5
Sample Client Code
A List would be used like this.
List data = new List(35);
data.add(35);
data.add(17);
data.add(42);
if (data.isMember(16))
System.out.println(“it’s broken!”);
else
System.out.println(“all’s well.”);
data.delete(35);
CS100A, Fall 1997, Lecture 12
6
Representation
A list will be implemented with an array to
store the values and an integer to keep track
of how many items are currently in the list.
class List
{private int nItems; // # of integers in the List
private int [ ] items; // integers in the List
// are stored in
// items[0..nItems-1];
A List object:
nItems
items
0
1
2
3
…
methods: List, add, delete, …
CS100A, Fall 1997, Lecture 12
7
Constructors
Task: Allocate an array of the requested (or
default) size, and set the list to empty.
// construct empty List of size 100
public List ( ) {
nItems = 0;
items = new int[100];
}
// construct empty List of size n
public List (int n) {
nItems = 0;
items = new int[n];
}
CS100A, Fall 1997, Lecture 12
8
MakeEmpty and Size
These operations are trivial
// make the list empty
public void makeEmpty( ) {
nItems = 0;
}
// yield the number of items in the list
public int size( ) {
return nItems;
}
CS100A, Fall 1997, Lecture 12
9
Searching the List — Find
The add, delete, and isMember operations
each need to search the list to locate a
number or determine it is not present. We
add a private method to the class to perform
the search and use it as needed.
// yield location k containing n (n=items[k])
// if n is in the List, otherwise yield nItems.
private int find(int n) {
int k = 0;
// inv: n is not in items[0..k-1]
while (k != nItems && items[k] != n)
k = k + 1;
return k;
}
CS100A, Fall 1997, Lecture 12
10
Test
nItems
3
items 35 17 42
0
1
2
3
…
methods: List, add, delete, find, …
CS100A, Fall 1997, Lecture 12
11
Add
To add an item, we first determine if it is in
the list. If not, and if the list isn’t already
full, we add it at the end.
// add n to the list if it is not present and there
// is room, otherwise do nothing.
public void add(int n) {
int loc; // if loc<nItems, then items[loc]=n,
//otherwise n is not in the list
loc = find(n);
if (loc == nItems &&
nItems < items.length) {
items[nItems] = n;
nItems = nItems + 1;
}
}
CS100A, Fall 1997, Lecture 12
12
Test
nItems
3
items 35 17 42
0
1
2
3
…
methods: List, add, delete, find, …
CS100A, Fall 1997, Lecture 12
13
Delete
To delete an item, we first check that it’s in
the list. If so, we move the last item in the
list to its place (to fill the “hole” in the list)
and decrease the list size.
// remove n from the List if it is present,
// otherwise do nothing.
public void delete(int n) {
int loc; // if loc<nItems, then items[loc]=n,
//otherwise n is not in the list
loc = find(n);
if (loc < nItems) {
items[loc] = items[nItems-1];
nItems = nItems -1;
}
}
CS100A, Fall 1997, Lecture 12
14
Test
nItems
5
items 35 17 42 18
0
1
2
3
21
…
methods: List, add, delete, find, …
CS100A, Fall 1997, Lecture 12
15
isMember
Membership testing is easy: find(n) < nItems
if n is in the list.
// yield true if n is a member of the List,
// otherwise yield false.
public boolean isMember(int n) {
return find(n) < nItems;
}
CS100A, Fall 1997, Lecture 12
16
toString
Finally, toString concatenates the integers in
the list into a string.
// yield a string representation of the List
public String toString( ) {
String rep; // string representation of List
int k;
rep = "[List:";
k = 0;
while (k < nItems) {
rep = rep + " " + items[k];
k = k + 1;
}
rep = rep + "]";
return rep;
}
CS100A, Fall 1997, Lecture 12
17