gpt4 book ai didi

java - 如何将具有给定索引的新元素添加到链表中?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:06:38 25 4
gpt4 key购买 nike

我正在学习 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;
}

旅游元素

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:失败

错误是:array first different at 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 时通过。但我不明白为什么第二个测试用例没有通过。请帮助我!!

最佳答案

如果您使用调试器运行,则可以检测到该问题。因此,首先,我不知道您使用的是哪个 IDE(如果有的话),但我强烈建议您熟悉这个工具。

现在,调试您的代码,我发现当您想在列表中的最后一个元素之前添加一个元素时会出现问题(失败的测试用例就是这种情况)。你的循环 while(current.next != null) 在最后一个元素处停止。你假设此时你想在列表的末尾插入,而实际上你想在最后一个元素之前插入。由于这是一个学习练习,我会让你自己解决这个问题。

还有一点评论:当在列表中间添加元素时,您在已经完成插入后不必要地继续迭代循环。

祝你好运!

关于java - 如何将具有给定索引的新元素添加到链表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56413770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com