CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays,...
Transcript of CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays,...
Array
8 February 2019 OSU CSE 1
Array
• The Array component family allows you to manipulate arrays in a way that overcomes surprising limitations of built-in Java arrays, but retains the time/space performance of built-in arrays– Another generic type like Sequence and Set– A best practice alternative from the OSU
CSE components to the built-in Java array (more like built-in arrays than Sequence is)
8 February 2019 OSU CSE 2
• Suppose you want to declare an array of type T, where T is a generic parameter, e.g.:T[] table = new T[100];
• Oops; this won’t compile!– The reason is complicated, having to do with
backward compatibility when Java generics were introduced and the inability to call a constructor for type T, among other oddities
8 February 2019 OSU CSE 3
Problems With Built-in Arrays
• Or, suppose you want to declare an array of type Queue<T>, e.g.:Queue<T>[] table = new Queue<T>[100];
• Oops; this won’t compile either!– Yet this is exactly the kind of thing you want to
do when creating a hashtable: the buckets are collections of a parameterized type
8 February 2019 OSU CSE 4
Problems With Built-in Arrays
Workarounds
• There are various “workarounds” that can be used, but they are ugly and generate warnings that you should generally heed rather than ignore
• These problems do not arise with the Array component family, so it can (and should) be used in many cases where you might be inclined to try built-in Java arrays
8 February 2019 OSU CSE 5
The Intuition
• The rather simple idea is to provide a generic interface Array<T> that provides the same functionality as built-in arrays, but where the entries may be of any type T
• The data representation used in Array1L<T> is a built-in array– But this data representation is hidden from the
client, along with one of the workarounds mentioned earlier
8 February 2019 OSU CSE 6
Interfaces and Classes
ArrayKernel
extends
Standard
extends
8 February 2019 OSU CSE 7
Array
implements
Array1L
Iterable
extends
Interfaces and Classes
8 February 2019 OSU CSE 8
ArrayKernel
extends
Standard
extends
Array
implements
Array1L
Iterable
extends
ArrayKernel has contracts for four
methods:setEntryentry
mayBeExaminedlength
Interfaces and Classes
8 February 2019 OSU CSE 9
ArrayKernel
extends
Standard
extends
Array
implements
Array1L
Iterable
extendsArray has contracts for
two methods:replaceEntry
exchangeEntries
Interfaces and Classes
8 February 2019 OSU CSE 10
ArrayKernel
extends
Standard
extends
Array
implements
Array1L
Iterable
extends
There is really an abstract class as usual in the chain
here, but it is not shown because these slides
describe the client view, and a client needs only interface Array and class Array1L.
Mathematical ModelARRAY_MODEL is (
entries: string of T,
examinableIndices: finite set of integer)exemplar a
constraintfor all i: integer
where (i is in a.examinableIndices)(0 <= i and i < |a.entries|)
type ArrayKernel is modeled by ARRAY_MODEL
8 February 2019 OSU CSE 11
Mathematical ModelARRAY_MODEL is (
entries: string of T,
examinableIndices: finite set of integer)exemplar a
constraintfor all i: integer
where (i is in a.examinableIndices)(0 <= i and i < |a.entries|)
type ArrayKernel is modeled by ARRAY_MODEL
8 February 2019 OSU CSE 12
The set examinableIndiceskeeps track of the indices in the Array that have been “initialized” by a call to setEntry, because
only these indices may be examined by a call to entry.
Constructor from int
• There is no no-argument constructor, but there is a constructor with one parameter int n
• Requires:n >= 0
• Ensures:|this.entries| = n andthis.examinableIndices = {}
8 February 2019 OSU CSE 13
Example
8 February 2019 OSU CSE 14
Code State
Array<Integer> ai =new Array1L<>(3);
Example
8 February 2019 OSU CSE 15
Code State
Array<Integer> ai =new Array1L<>(3);
ai = (<?, ?, ?>,{})
Example
8 February 2019 OSU CSE 16
Code State
Array<Integer> ai =new Array1L<>(3);
ai = (<?, ?, ?>,{})
All we know from the constructor contract is that the length of thisstring of integeris 3; we do not know its
actual value.
setEntryvoid setEntry(int i, T x)• Sets the entry at index i of this to x.• Aliases: reference x• Updates: this• Requires:
0 <= i and i < |this.entries|
• Ensures:this.entries = #this.entries[0, i) * <x> *
#this.entries[i+1, |#this.entries|) andthis.examinableIndices = #this.examinableIndices
union {i}
8 February 2019 OSU CSE 17
Example
8 February 2019 OSU CSE 18
Code Stateai = (<?, ?, ?>,
{})z = 70
ai.setEntry(1, z);
Example
8 February 2019 OSU CSE 19
Code Stateai = (<?, ?, ?>,
{})z = 70
ai.setEntry(1, z);
ai = (<?, 70, ?>,{1})
z = 70
Example
8 February 2019 OSU CSE 20
Code Stateai = (<?, ?, ?>,
{})z = 70
ai.setEntry(1, z);
ai = (<?, 70, ?>,{1})
z = 70
Note the alias created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it.
Another Example
8 February 2019 OSU CSE 21
Code Stateai = (<?, -8, ?>,
{1})z = 70
ai.setEntry(1, z);
Another Example
8 February 2019 OSU CSE 22
Code Stateai = (<?, -8, ?>,
{1})z = 70
ai.setEntry(1, z);
ai = (<?, 70, ?>,{1})
z = 70
entry
T entry (int i)
• Reports the entry at index i of this.• Aliases: reference returned by entry• Requires:i is in this.examinableIndices
• Ensures:<entry> = this.entries[i, i+1)
8 February 2019 OSU CSE 23
entry
T entry(int i)
• Reports the entry at position i of this.• Aliases: reference returned by entryAt• Requires:i is in this.examinableIndices
• Ensures:<entry> = this.entries[i, i+1)
8 February 2019 OSU CSE 24
Note that this implies the index i is “within bounds”, i.e.:
0 <= i andi < |this.entries|
Example
8 February 2019 OSU CSE 25
Code Stateai = (<?, 70, -3>,
{1, 2})z = –584
z = ai.entry(1);
Example
8 February 2019 OSU CSE 26
Code Stateai = (<?, 70, -3>,
{1, 2})z = –584
z = ai.entry(1);
ai = (<?, 70, -3>,{1, 2})
z = 70
Example
8 February 2019 OSU CSE 27
Code Stateai = (<?, 70, -3>,
{1, 2})z = –584
z = ai.entry(1);
ai = (<?, 70, -3>,{1, 2})
z = 70
Note the alias created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it.
mayBeExamined
boolean mayBeExamined(int i)
• Reports whether the entry at index i of this may be examined using entry.
• Ensures:mayBeExamined =
i is in this.examinableIndices
8 February 2019 OSU CSE 28
Example
8 February 2019 OSU CSE 29
Code State
ai = (<?, 70, -3>,{1, 2})
boolean ok =ai.mayBeExamined(1);
Example
8 February 2019 OSU CSE 30
Code State
ai = (<?, 70, -3>,{1, 2})
boolean ok =ai.mayBeExamined(1);
ai = (<?, 70, -3>,{1, 2})
ok = true
length
int length()
• Reports the length of this.• Ensures:length = |this.entries|
8 February 2019 OSU CSE 31
replaceEntryT replaceEntry(int i, T x)• Replaces the entry at index i of this with x, and
returns the old entry at that index.• Aliases: reference x• Updates: this.entries• Requires:
i is in this.examinableIndices
• Ensures:this.entries = #this.entries[0, i) * <x> *
#this.entries[i+1, |#this|) and<replaceEntry> = #this.entries[i, i+1)
8 February 2019 OSU CSE 32
Example
8 February 2019 OSU CSE 33
Code Stateai = (<?, 70, -3>,
{1, 2})z = 58w = 94
w = ai.replaceEntry(2, z);
Example
8 February 2019 OSU CSE 34
Code Stateai = (<?, 70, -3>,
{1, 2})z = 58w = 94
w = ai.replaceEntry(2, z);
ai = (<?, 70, 58>,{1, 2})
z = 58w = -3
Example
8 February 2019 OSU CSE 35
Code Stateai = (<?, 70, -3>,
{1, 2})z = 58w = 94
w = ai.replaceEntry(2, z);
ai = (<?, 70, 58>,{1, 2})
z = 58w = -3
Note the alias created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it.
Another Example
8 February 2019 OSU CSE 36
Code Stateai = (<?, 70, -3>,
{1, 2})z = 58
z = ai.replaceEntry(2, z);
Another Example
8 February 2019 OSU CSE 37
Code Stateai = (<?, 70, -3>,
{1, 2})z = 58
z = ai.replaceEntry(2, z);
ai = (<?, 70, 58>,{1, 2})
z = -3
Another Example
8 February 2019 OSU CSE 38
Code Stateai = (<?, 70, -3>,
{1, 2})z = 58
z = ai.replaceEntry(2, z);
ai = (<?, 70, 58>,{1, 2})
z = -3
This use of the method avoids creating an alias: it
swaps z with the entry previously at index 2.
exchangeEntriesvoid exchangeEntries(int i, int j)• Exchanges entries at indices i and j of this.• Updates: this.entries• Requires:
{i, j} is subset of this.examinableIndices
• Ensures:this.entries = [#this.entries with entries
at indices i and j exchanged]
8 February 2019 OSU CSE 39
Example
8 February 2019 OSU CSE 40
Code State
ai = (<?, 70, -3>,{1, 2})
ai.exchangeEntries(1, 2);
Example
8 February 2019 OSU CSE 41
Code State
ai = (<?, 70, -3>,{1, 2})
ai.exchangeEntries(1, 2);
ai = (<?, -3, 70>,{1, 2})
iterator
Iterator<T> iterator()
• Returns an iterator over a set of elements of type T.
• Ensures:~this.seen * ~this.unseen =
[string of entries of this.entries,in order by increasing index, where
index is in this.examinableIndices]
8 February 2019 OSU CSE 42
Resources
• OSU CSE Components API: Array– http://cse.osu.edu/software/common/doc/
8 February 2019 OSU CSE 43