我在向通用链表类中实现add方法时遇到了一些麻烦,该类会在给定索引处添加一个值。但是,在通过测试类运行时,我无法正确执行此操作我遇到以下错误:
1) t31AddtoEmptyList(LinkedListTest$Add2Test)
java.lang.IndexOutOfBoundsException
2) t32AddtoThreeItemList0(LinkedListTest$Add2Test)
java.lang.AssertionError: four element list has wrong size. expected:<4> but was:<3>
3) t35AddtoThreeItemList3(LinkedListTest$Add2Test)
java.lang.IndexOutOfBoundsException
这是我在索引方法中的添加:
public void add(int index, E value){
if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException();
//if empty add to front
}else if(index == 0) {
front = new ListNode(value, front);
// if index is last element add to front
}else if(index == size){
add(value);
}else {
ListNode current = front;
for (int i = 0; i < index - 1; i++){
current = current.next;
}
current.next = new ListNode(value, current.next);
}
this.size++;
}
我全班:
public class LinkedList<E>{
int size;
private class ListNode{
E data;
ListNode next;
ListNode(E data) {
this.data = data;
next = null;
}
ListNode(E data, ListNode next) {
this.data = data;
this.next = next;
}
}
private ListNode front;
private ListNode end;
// Constructor
// Construct an empty LinkedList object.
public LinkedList(){
this.size = 0;
this.front = null;
}
// Return the size (number of items) in this LinkedList.
public int size(){
return size;
}
// Return true if this LinkedList has no items.
// Return false if the size is greater than zero.
boolean isEmpty(){
return size() == 0;
}
// Add the given element, value, to the end of the list.
// appends
public void add(E value){
if (front == null) {
front = new ListNode(value);
} else {
ListNode current = front;
while (current.next != null) {
current = current.next;
}
current.next = new ListNode(value);
}
this.size++;
}
// Add the given element, value, to the list at the given index.
// After this operation is complete, get(index) will return value.
// This operation is only valid for 0 <= index <= size().
public void add(int index, E value){
if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException();
//if empty add to front
}else if(index == 0) {
front = new ListNode(value, front);
// if index is last element add to front
}else if(index == size){
add(value);
}else {
ListNode current = front;
for (int i = 0; i < index - 1; i++){
current = current.next;
}
current.next = new ListNode(value, current.next);
}
this.size++;
}
/*
// prepend helper method
//prepend
public void prepend(int data){
if (front == null){
end = new ListNode();
front = end;
}
else {
front = new ListNode(data,front);
}
size++;
}
*/
// Return the element of the list at the given index.
// This operation is only valid for 0 <= index < size().
// This operation does not modify the list.
public E get(int index){
// check for index out of bounds
if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException();
}
// returns char at index
ListNode curr = front;
for (int i = 0; i < index; i++) {
curr = curr.next;
}
return curr.data;
}
} // end class
我的考试班https://gist.github.com/Silverfin13/e22d061c2daf0d3a7013
感谢您的帮助,谢谢
答案 0 :(得分:0)
if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException();
index >= this.size
顶部的public void add(int index, E value)
条件是问题所在
我想它应该是index > this.size
为什么呢?如果您想要在最后一个位置添加index
的节点,那么它将等于当前size
并且它有效。
您还有else if(index == size)
这个条件,因此它意味着您希望允许index
等于size
,但在顶部您要为new IndexOutOfBoundsException();
投放public void add(int index, E value)
这一点。
尺寸维持方面还有另一个问题。
this.size++;
此方法会增加任何有效插入的大小index==size
。但是对于else if(index == size){
add(value);
public void add(E value)
您调用index==size
方法,如果this.size
custom_theme
增加两倍,也会在操作结束时增加大小。