【问题标题】:How to append an element in the end of list structure in java?java - 如何在java列表结构的末尾追加一个元素?
【发布时间】:2019-10-14 19:32:28
【问题描述】:

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

我有 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}

【问题讨论】:

  • createElementList 是做什么的?
  • 如@Jonk 所问,请解释createWaypointcreateElementList 方法
  • 对不起,我刚刚编辑了我的代码。 createElementList 将创建一个包含很多点的 ElementList。 Waypoint 创建一个点看起来像这样:(1,2)。

标签: java data-structures linked-list append


【解决方案1】:

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

所以我想知道,您的测试用例如何返回完整列表?我希望“实际输出”只是“{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()并添加一个@987654332 @。

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(), ", ");

【讨论】:

  • 我明白了,测试用例是我老师提供的,所以我不知道。但是当不更改 elem 时,测试用例不会运行。我收到错误“null PointerException”。这是什么意思?
  • 你能回顾一下我的 append() 方法吗?我认为我的代码在这种方法中是错误的......
  • 好吧,我删除了elem = ,测试运行没有问题...代码:pastebin.com/z6w26ikw
  • 我不知道,但是当我试图删除元素时,测试没有运行。我不知道为什么。我使用 netbean 做作业....它出现错误“null PointerException”。而且因为测试用例是我们教授提供的,所以不知道自己在做什么run\
  • 好的。由于提供了测试用例,您可以尝试不通过再次返回“elem”来更改“elem”引用。这可以通过在append() 中返回“elem”来执行。 elemthis 相同。尝试将append()的最后一行替换为return this;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 2011-08-19
  • 1970-01-01
  • 2020-09-11
  • 2023-04-05
  • 2021-01-21
  • 1970-01-01
相关资源
最近更新 更多