【发布时间】:2019-05-24 09:46:32
【问题描述】:
我正在尝试编写一个名为 addStart() 的函数来在列表结构的第一个节点的前面添加新元素。请帮我弄清楚。
我有 2 个名为 Waypoint 的类和 TourElement 类。 Waypoint 提供了使用点的方法。一个 Tour Element 包含很多点。
//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;
}
//Tour Element.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;
}
//我需要关于 addStart 函数的帮助 //,它在第一个元素前面添加路点。如果参数为空,则返回列表不变。
TourElement addStart(Waypoint wp) {
if(this.next == null)
{
TourElement newTourElement = new TourElement();
newTourElement.setWaypoint(wp);
this.next = newTourElement;
}
return this;
}
//addStart的测试用例:
public void test0AddStart() {
TourElement elem = new TourElement();
elem.setWaypoint(createWaypoint(2, 2));
elem = elem.addStart(createWaypoint(1, 1));
assertArrayEquals(new int[] {1, 1}, elem.getWaypoint().toArray());
assertArrayEquals(new int[] {2, 2}, elem.getNext().getWaypoint().toArray());
assertNull(elem.getNext().getNext());
}
我的输入是一个节点列表,例如:{1,2} -> {2,3} -> {3,4} 和一个航点 {5,6}。我希望我的输出是:{5,6 } -> {1,2} -> {2,3} -> {3,4}
【问题讨论】:
标签: java list linked-list structure