gpt4 book ai didi

java - 如何在java中的列表结构末尾追加一个元素?

转载 作者:行者123 更新时间:2023-12-02 05:05:16 25 4
gpt4 key购买 nike

我正在尝试编写一个函数来在列表末尾 append 一个新元素。但我不知道我的方法如何总是在列表的第一个索引中 append 新元素。

我有 2 个类,名为 Waypoint 和 TourElement。 Waypoint 包含处理链表中的点的方法。 TourElment 包含航点和下一个。

Waypoint.java

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;
}
}

TourElement.java

 public class TourElement {
private Waypoint points;
private TourElement next;
public void setWaypoint( Waypoint points)
{
this.points = points;
}
public void setTourElement(TourElement next)
{
this.next = next;
}
Waypoint getWaypoint()
{
return this.points;
}

TourElement getNext()
{
return this.next;
}

//+ method below
}

方法在列表中添加新的 TourElement,为什么总是将 TourElement 添加到列表的第一个索引中?

public TourElement append(Waypoint waypoint)
{
TourElement newTourElement = new TourElement();
TourElement current = this;
while(current.next != null)
{
current = current.next;
}
newTourElement.setWaypoint(waypoint);
current.next = newTourElement;
return newTourElement;
}

这是我的测试用例://创建一个元素列表:

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;
}

//创建一个路径点:

private Waypoint createWaypoint(int x, int y) {
Waypoint wp = new Waypoint();
wp.setXY(x, y);
return wp;
}

//添加开始

public   TourElement addStart(Waypoint wp) {
TourElement newTourElement = new TourElement();
newTourElement.setWaypoint(wp);
newTourElement.setTourElement(this);
return newTourElement;
}



public void testAppend() {
TourElement elem = createElementList(new int[][] {{2, 2}});
elem = elem.append(createWaypoint(3, 3));
assertArrayEquals(new int[] {2, 2}, elem.getWaypoint().toArray());
assertArrayEquals(new int[] {3, 3}, elem.getNext().getWaypoint().toArray());
assertNull(elem.getNext().getNext());
}

public void testAppend_AfterTwo() {
TourElement elem = createElementList(new int[][] {{1, 1}, {2, 2}});
elem = elem.append(createWaypoint(3, 3));
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());
}

我希望输出如下所示:

测试用例 1:{2,2} => {3,3}

测试用例 2:{1,1} => {2,2} =>{3,3}

但我的实际输出是:

测试用例 1:{3, 3} => {2,2}

测试用例 2:{3,3} =>{1,1} =>{2,2}

最佳答案

您正在更新 elem 行中的“elem = elem.append(createWaypoint(3, 3)); ” .

所以我想知道,您的测试用例如何返回完整列表?我预计“实际输出”仅为“{3,3} ”。

要解决此问题,测试不应更改 elem变量根本没有,在它首先被初始化之后:

 public void testAppend() {
// add "final" to prevent accidentially changes of references
final TourElement elem = createElementList(new int[][] {{2, 2}});

// DO NOT CHANGE "elem"!
// elem = elem.append(createWaypoint(3, 3));
// instead keep the reference to the first elem:
elem.append(createWaypoint(3, 3));

assertArrayEquals(new int[] {2, 2}, elem.getWaypoint().toArray());
assertArrayEquals(new int[] {3, 3}, elem.getNext().getWaypoint().toArray());
assertNull(elem.getNext().getNext());
}

顺便说一句:如果你给“Waypoint”一个构造函数“Waypoint(x, y)”,你的代码可能会更清晰,使xy最终字段,删除 setXY()并添加 toString() .

public class Waypoint {
final int x;
final int y;
public WayPoint(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() { ... }
public int getY() { ... }

@Override
public String toString() {
return String.format("{%d,%d}", x, y);
}
}

您的TourElement可能需要 Collection<Waypoint> waypoints()方法,返回后续 Waypoint对象(使测试更容易)。

public Collection<Waypoint> waypoints() {
Collection<Waypoint> res = new java.util.ArrayList<>();
TourElement currTE = this;
while (currTE.next()!=null) {
res.add(currTE.getWaypoint());
currTE = currTE.next();
}
return res;
}

然后打印 Waypoint 的列表,你只需这样做

    org.apache.commons.lang.StringUtils.join(elem.waypoints(), ", ");

关于java - 如何在java中的列表结构末尾追加一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56356302/

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