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

Java 如何将具有给定索引的新元素添加到链表中?,java,algorithm,indexing,linked-list,structure,Java,Algorithm,Indexing,Linked List,Structure,我正在学习Java中的链表,并尝试制作一个函数,用给定的索引向链表中添加新的点。请帮我检查代码,然后告诉我这里做错了什么。提前非常感谢 我的程序有两个类名Waypoint和TourElement。我还有一些测试用例 航路点 public class Waypoint { int x; int y; public int getX() { return this.x; } public int getY() { retur

我正在学习Java中的链表,并尝试制作一个函数,用给定的索引向链表中添加新的点。请帮我检查代码,然后告诉我这里做错了什么。提前非常感谢

我的程序有两个类名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

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());
}
@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());
}
测试用例2:失败

@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());
}
@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());
}
错误是:数组首先在元素[0]处不同;预期但事实是:

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;
    }
测试用例3:通过

@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());
}
@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)
在最后一个元素处停止。假设此时您希望在列表末尾插入,而实际上,您希望在最后一个元素之前插入。由于这是一个学习练习,我将让你自己解决这个问题

另一个注释:当在列表中间添加元素时,在已经完成插入之后,不必要地重复循环。


祝你好运

测试用例2在哪一点上失败了?出现了什么消息?您需要向我们显示
createElementList()
,因为您正在使用它的返回值进行插入。很抱歉,我编辑了代码并在测试用例上方显示createElementList()。我真的不明白这一点。我的作业使用netbean 8.2。关于您的想法,我知道while(current.next!=null)在最后一个元素处停止,并且我有一个条件if(index==count)在最后一个元素之前插入。我在这段代码中做错了什么?所以使用IDE内置调试器来运行代码我知道错误,但我不知道如何修复代码。看起来当我运行函数时,它不能在给定的索引处插入新元素。如果你知道这个问题,请告诉我我做错了什么..我试图在我的回答中解释这个问题-问题是当你试图在列表中最后一个元素之前插入一个元素到索引中时,请查看
If(index==count)
。您正在调用
newElement.setNext()
两次,这不可能是正确的