我正在学习Java中的链接列表,并试图创建一个函数以给定索引将新点添加到链接列表中。请帮助我检查代码,然后在这里告诉我我做错了什么。提前非常感谢您!!
我的程序有2个类名Waypoint和TourElement。我也有一些测试用例。
航点
public class Waypoint {
int x;
int y;
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void setXY(int x, int y) {
this.x = x;
this.y = y;
}
int[] toArray() {
int array[] = new int[2];
array[0] = getX();
array[1] = getY();
return array;
}
@Override
public String toString() {
String convertToString = "(" + getX() + "/" + getY() + ")";
return convertToString;
}
TourElement
public class TourElement {
private Waypoint points;
private TourElement next;
public void setWaypoint( Waypoint points) {
this.points = points;
}
public void setNext(TourElement next) {
this.next = next;
}
Waypoint getWaypoint() {
return this.points;
}
TourElement getNext() {
return this.next;
}
boolean hasNext() {
if(this.next != null) {
return true;
}
return false;
}
int getNoOfWaypoints() {// return the number of waypoints in the list
int count = 1;
TourElement current = this;
while(current.next != null) {
count++;
current = current.next;
System.out.println(count);
}
return count;
}
这是插入具有给定索引的新点的功能:
TourElement insertAt(int index, Waypoint waypoint) {
int lengthLinkList = getNoOfWaypoints();
TourElement current = this;
int count = 0;
if(waypoint == null || index < 0 || index > lengthLinkList) {
return this;
} else {
if(index == 0) {
TourElement newElement = new TourElement();
newElement.setWaypoint(waypoint);
newElement.setNext(this);
return newElement;
} else {
while(current.next != null) { //I think I'm doing wrong here when trying to add new points.
if(index == count) {
TourElement newElement = new TourElement();
current.setNext(current);
newElement.setWaypoint(waypoint);
newElement.setNext(current.next);
return newElement;
}
count++;
current = current.next;
}
if(current.next == null) {
TourElement newElement = new TourElement();
current.setNext(newElement);
newElement.setWaypoint(waypoint);
newElement.setNext(null);
}
}
return this;
}
}
这是我的测试用例: //创建元素列表:
private Waypoint createWaypoint(int x, int y) {
Waypoint wp = new Waypoint();
wp.setXY(x, y);
return wp;
}
/**
* Creates a ElementList with the given waypoints.
* @param waypoints array of waypoints to use, the coordinates of the waypoints are also in an array
* @return List of elements with the given waypoints
* @pre at least one waypoint has to be in array
*/
private TourElement createElementList(int[][] waypoints){
assert waypoints.length > 0;
TourElement elem = new TourElement();
int lastIndex = waypoints.length-1;
Waypoint wp = createWaypoint(waypoints[lastIndex][0], waypoints[lastIndex][1]);
elem.setWaypoint(wp);
for (int i = lastIndex-1; i >= 0 ; i--) {
wp = createWaypoint(waypoints[i][0], waypoints[i][1]);
elem = elem.addStart(wp);
}
return elem;
}
测试案例1:通过
@Test
public void testInsertAt_BeforeFirst() {
TourElement elem = createElementList(new int[][] {{1, 1}, {3, 3}});
Waypoint wp = createWaypoint(0, 0);
elem = elem.insertAt(0, wp);
assertArrayEquals(new int[] {0, 0}, elem.getWaypoint().toArray());
assertArrayEquals(new int[] {1, 1}, elem.getNext().getWaypoint().toArray());
assertArrayEquals(new int[] {3, 3}, elem.getNext().getNext().getWaypoint().toArray());
assertNull(elem.getNext().getNext().getNext());
}
测试案例2:失败
错误是:数组首先在element [0]处不同;预期为<2>,但预期为:<3>
@Test
public void testInsertAt_Second() {
TourElement elem = createElementList(new int[][] {{1, 1}, {3, 3}});
Waypoint wp = createWaypoint(2, 2);
elem = elem.insertAt(1, wp);
assertArrayEquals(new int[] {1, 1}, elem.getWaypoint().toArray());
assertArrayEquals(new int[] {2, 2}, elem.getNext().getWaypoint().toArray());
assertArrayEquals(new int[] {3, 3}, elem.getNext().getNext().getWaypoint().toArray());
assertNull(elem.getNext().getNext().getNext());
}
测试案例3:通过
@Test
public void testInsertAt_Last() {
TourElement elem = createElementList(new int[][] {{1, 1}, {3, 3}});
Waypoint wp = createWaypoint(4, 4);
elem = elem.insertAt(2, wp);
assertArrayEquals(new int[] {1, 1}, elem.getWaypoint().toArray());
assertArrayEquals(new int[] {3, 3}, elem.getNext().getWaypoint().toArray());
assertArrayEquals(new int[] {4, 4}, elem.getNext().getNext().getWaypoint().toArray());
assertNull(elem.getNext().getNext().getNext());
}
在函数insertAt(index, waypoint)
中,当index = 0或index = last时,此函数通过。但是我不明白为什么第二个测试用例没有通过。
请帮帮我!
答案 0 :(得分:1)
如果使用调试器运行,则可以检测到该问题。因此,首先,我不知道您使用的是哪个IDE(如果有的话),但是我强烈建议您熟悉此工具。
现在,调试代码,我发现当您要在列表中的最后一个元素之前添加元素时(与失败的测试用例一样),就会出现问题。
您的循环while(current.next != null)
停在最后一个元素。您假设此时要插入列表的末尾,而实际上您要插入最后一个元素的前。由于这是一项学习练习,我将让您自己解决这个问题。
另外一条评论:在列表的中间添加元素时,在您已经执行插入操作之后,您无需重复进行循环。
祝你好运!