如何在Java中的列表结构末尾添加元素?

时间:2019-05-29 08:35:14

标签: java data-structures linked-list append

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

我有两个名为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}

1 个答案:

答案 0 :(得分:0)

您正在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字段定为final,然后删除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(), ", ");