三个测试程序之一不起作用。它为我提供了一些不起作用的提示。我想不通。我很确定这很简单。
这是我的错误
Exception in thread "main" java.lang.RuntimeException:
while adding initial numbers to list,
size = 0 when it should be 1
test size = 1
your list = []
(if your size does match the number of elements in your list,
this error probably means you have not properly added the
number of elements requested by the test case.)
at Test3.checkSize(Test3.java:142)
at Test3.testTrue(Test3.java:57)
at Test3.main(Test3.java:30)
这是我的程序
import java.util.*;
public class SortedIntList {
private int[] elementData; // list of integers
private int size; // current number of elements in the list
private boolean unique;
public static final int DEFAULT_CAPACITY = 100;
// post: constructs an empty list of default capacity
public SortedIntList() {
this(DEFAULT_CAPACITY);
}
// pre : capacity >= 0 (throws IllegalArgumentException if not)
// post: constructs an empty list with the given capacity
public SortedIntList(int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("capacity: " + capacity);
}
elementData = new int[capacity];
size = 0;
}
public SortedIntList(boolean unique) {
this.unique = unique;
elementData = new int[DEFAULT_CAPACITY];
size = 0;
}
// pre : capacity >= 0 (throws IllegalArgumentException if not)
// post :
public SortedIntList(boolean unique, int capacity) {
this.unique = unique;
if (capacity < 0) {
throw new IllegalArgumentException("capacity: " + capacity);
}
elementData = new int[capacity];
size = 0;
}
// post: returns the current number of elements in the list
public int size() {
return size;
}
// pre : 0 <= index < size() (throws IndexOutOfBoundsException if not)
// post: returns the integer at the given index in the list
public int get(int index) {
checkIndex(index);
return elementData[index];
}
// post: creates a comma-separated, bracketed version of the list
public String toString() {
if (size == 0) {
return "[]";
} else {
String result = "[" + elementData[0];
for (int i = 1; i < size; i++) {
result += ", " + elementData[i];
}
result += "]";
return result;
}
}
// post : returns the position of the first occurrence of the given
// value (-1 if not found)
public int indexOf(int value) {
int index = Arrays.binarySearch(elementData, 0, size, value);
return index;
}
// post: returns true if list is empty, false otherwise
public boolean isEmpty() {
return size == 0;
}
// post: returns true if the given value is contained in the list,
// false otherwise
public boolean contains(int value) {
return indexOf(value) >= 0;
}
// pre : 0 <= index < size (throws IndexOutOfBoundsException if not)
// post: places the value at the given index in the list
public void set(int index, int value) {
checkIndex(index);
elementData[index] = value;
}
// pre : 0 <= index <= size() (throws IndexOutOfBoundsException if not)
// post: inserts the given value at the given index, shifting subsequent
// values right
private void add(int index, int value) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("index: " + index);
}
ensureCapacity(size + 1);
for (int i = size; i > index; i--) {
elementData[i] = elementData[i - 1];
}
elementData[index] = value;
size++;
}
// post: adds the value in the appropriate place to keep the list in sorted order
public void add(int value) {
if (unique == false) {
int index = Arrays.binarySearch(elementData, 0, size, value);
index = indexAdjust(index);
add(index, value);
} else { // (unique == true)
if (contains(value) == false) {
int index = Arrays.binarySearch(elementData, 0 , size, value);
index = indexAdjust(index);
if (elementData[index] != value) {
add(index, value);
}
}
}
}
// post : index is changed to the proper positive value from binarySearch result
private int indexAdjust(int index) {
if (index < 0) {
index = -index - 1;
}
return index;
}
// post: list is empty
public void clear() {
size = 0;
}
// post: ensures that the list has the given capacity; if not, the size is
// doubled (or more if given capacity is even larger)
public void ensureCapacity(int capacity) {
if (capacity > elementData.length) {
int newCapacity = elementData.length * 2 + 1;
if (capacity > newCapacity) {
newCapacity = capacity;
}
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
// pre : 0 <= index < size() (throws IndexOutOfBoundsException if not)
// post: removes value at the given index, shifting subsequent values left
public void remove(int index) {
checkIndex(index);
for (int i = index; i < size - 1; i++) {
elementData[i] = elementData[i + 1];
}
size--;
}
// post: throws an IndexOutOfBoundsException if the given index is
// not a legal index of the current list
private void checkIndex(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("index: " + index);
}
}
// pre : throws an IndexOutOfBoundsException if the size is zero
// post : returns the min/first value in the list
public int min() {
if (size == 0) {
throw new NoSuchElementException("empty array");
}
return elementData[0];
}
// pre : throws an IndexOutOfBoundsException if the size is zero
// post : returns the max/last value in the list
public int max() {
if (size == 0) {
throw new NoSuchElementException("empty array");
}
return elementData[size - 1];
}
// post : returns current setting of unique
public boolean getUnique() {
return unique;
}
// post : unique is set the boolean value, and duplicates are removed if present
public void setUnique(boolean value) {
System.out.println(Arrays.toString(elementData));
if (value == true) {
for (int i = 0; i < size; i += 1) {
System.out.println(Arrays.toString(elementData));
int first = elementData[i];
int second = elementData[i + 1];
if (first == second) {
remove(i);
i--;
}
}
unique = true;
} else { // value == false
unique = false;
}
}
}