Due: Thursday 12/12 10:00am
Submission name: work31_listarray
Create a ListArray
class with the following features:
- A
ListArray
should storechar
values using an array. - Include the following methods:
ListArray(int capacity)
: Constructor that sets the initial capacity of the underlying array.ListArray()
: Overloaded constructor, set the initial capacity to 20.String toString()
: Return a string showing all the elements in the list, enclosed by[]
, with spaces between.- e.g.
[ ]
or[ A B C ]
- e.g.
void add(char c)
: Appendc
to the end of the list. (assume there is enough space in the underlying array)char get(int i)
: Return the value at indexi
. Ifi
is invalid, return0
.int indexOf(char c)
: Return the index ofc
. Ifc
is not in the list, return-1
.
Use the following driver file code to test your class:
ListArray la;
void setup() {
la = new ListArray();
populate(la, 10, true);
println(la.toString());
int i = la.indexOf('C');
int j = la.indexOf('?');
println("index of c: " + i + " " + la.get(i));
println("index of ?: " + j + " " + la.get(j));
}//setup
void populate(ListArray s, int count, boolean ordered) {
for (int i=0; i < count; i++) {
char c = char('A' + i);
if (!ordered) {
c = char(int(random(26)) + 'A');
}
s.add(c);
}//loop
}//populate