我已经创建了数组部分并且稍微启动了测试器应用程序来测试数组是否正常工作..
但是,对于以下方法:
void clear()
我想知道如何围绕这些
创建测试人员基于数组的应用程序:
public class ListOfStrings {
public static final int DEFAULT_CAPACITY = 4;
private String[] names;
private int size = 0;
public ListOfStrings() {
this(DEFAULT_CAPACITY);
}
public ListOfStrings(int initialCapacity) {
names = new String[initialCapacity];
}
public int getCapacity() {
return names.length;
}
public int getSize() {
return size;
}
// Adds name to the end of the list
public void add(String name) {
if (this.getSize() == this.getCapacity()) {
// Double the size of the array
String[] temp = new String[this.getCapacity() * 2];
System.arraycopy(names, 0, temp, 0, this.getCapacity());
names = temp;
}
names[size] = name;
size++;
}
// set item i to the given name
public void set(int i, String name) {
if (i < 0 || i > this.getSize() - 1) {
// we have a problem
String message = "index " + i + " not valid";
throw new IndexOutOfBoundsException(message);
}
names[i] = name;
}
// returns the item at index i
public String get(int i) {
if (i < 0 || i > this.getSize() - 1) {
// we have a problem
String message = "index " + i + " not valid";
throw new IndexOutOfBoundsException(message);
}
return names[i];
}
// removes and returns item i from the list
public String remove(int i) {//
if (i < 0 || i > this.getSize() - 1) {
// we have a problem
String message = "index " + i + " not valid";
throw new IndexOutOfBoundsException(message);
}
String removedItem = this.get(i); // save item to return
// now adjust the array
if (i < size - 1) {
System.arraycopy(names, i + 1, names, i, size - i - 1);
}
size--;
return removedItem;
}
@Override
public String toString() {
String s = "";
s += "[";
boolean first = true;
for (int i = 0; i < size; i++) {
if (first) {
s += names[i];
first = false;
} else {
s += ", ";
s += names[i];
}
}
s += "]";
return s;
}
/*
* Removes and returns the first string in the list. If the list is empty,
* null is returned.
*/
public String removeFirst() {
if (size < 1)
return null;
String removeItem = this.get(0);
this.remove(0);
return removeItem;
}
/*
* Removes and returns the last string in the list. If the list is empty,
* null is returned.
*/
public String removeLast() {
if (size < 1)
return null;
String removeItem = this.get(size - 1);
this.remove(size - 1);
size--;
return removeItem;
}
/*
* Removes all unused array elements from the end of the arry, if any exit.
* After calling this method the size and the capacity of the list should be
* same.
*/
public void compress() {
if (size == DEFAULT_CAPACITY)
return;
if (size == 0) {
this.names = new String[] {};
return;
}
String[] newList = new String[size];
System.arraycopy(names, 0, newList, 0, size);
this.names = newList;
}
/*
* Increases the capacity, if needed, to new specified capacity. This may
* mean making a new, lager array.
*/
public void ensureCapacity(int newCapacity) {
if (newCapacity <this.getCapacity()) {
String message = "The new capacity must be lager than the original capacity("
+ this.getCapacity() + ").";
throw new IndexOutOfBoundsException(message);
}
String[] newList = new String[newCapacity];
if (size > 0)
System.arraycopy(names, 0, newList, 0, size);
this.names = newList;
}
/*
* Returns the index of the first occurrence of specified string. If the
* string is not in the list, returns -1.
*/
public int getIndex(String s) {
if (size == 0) {
return -1;
}
for (int i = 0; i < size; i++) {
if (get(i).equals(s)) {
return i;
}
}
return -1;
}
/*
* Removes and returns the first occurrence of the specified string. If the
* String is not in the list, returns null;
*/
public String remove(String s) {
for (int i = 0; i < size; i++) {
if (get(i).equals(s)) {
this.remove(i);
return s;
}
}
return null;
}
/* Removes all strings from the list and set the capacity to the default capacity.
*
* */
public void Clear() {
names = new String[DEFAULT_CAPACITY];
size = 0;
}
}
当前的测试人员正在进行中:
public class ListTester {
public static void main(String[] args) {
ListOfStrings L = new ListOfStrings();
// Add items until the capacity is increased
System.out.println("\n------- Add List Items -----------------");
printList(L);
L.add("A");
printList(L);
L.add("B");
printList(L);
L.add("C");
printList(L);
L.add("D");
printList(L);
L.add("E");
printList(L);
// Get List items
System.out.println("\n------- Get List Items -----------------");
printList(L);
System.out.println("List item 0: " + L.get(0));
System.out.println("List item 2: " + L.get(1));
System.out.println("Last list item: " + L.get(L.getSize() - 1));
printList(L);
// Set a list item
System.out.println("\n------- Set List Items -----------------");
L.set(2, "zzz");
printList(L);
System.out.println("\n------- Remove List Items --------------");
printList(L);
System.out.println("Remove item 2");
String item = L.remove(2);
System.out.println("removed item: " + item);
printList(L);
System.out.println();
System.out.println("Remove item 3");
item = L.remove(3);
System.out.println("removed item: " + item);
printList(L);
System.out.println();
System.out.println("Remove item 0");
item = L.remove(0);
System.out.println("removed item: " + item);
printList(L);
}
public static void printList(ListOfStrings lst){
System.out.print("size: " + lst.getSize());
System.out.print(" capacity: " + lst.getCapacity());
System.out.print(" List: " + lst);
System.out.println();
}
}