简体   繁体   English

我如何使用java中的list Structures确定java中当前元素之后是否存在其他元素

[英]How I can determine if there is another element after the current one or not in java using list Structures in java

I'm trying to write an function named boolean hasNext() which checks if there is another element after the current one or not I have an class named TourElement, it contains a lot of points. 我正在尝试编写一个名为boolean hasNext()的函数,它检查当前的元素之后是否还有另一个元素我是否有一个名为TourElement的类,它包含很多点。 Here is my code // class waypoint: 这是我的代码// class waypoint:

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

//class tourElement // class tourElement

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

    boolean hasNext(Waypoint first){
    // What am I doing wrong here?
        TourElement current = getNext();
        while( current.next != null)
        {
            return true;
        }
        return false;

    }
    // my test case
         public void testHasNext()
        {
           TourElement elem = createElementList(new int[][] {{0, 0}, {1, 1}, {2, 2}});

            assertEquals(true,elem.hasNext(createWaypoint(1, 1)));
        }

//create element list: //创建元素列表:

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

// create waypoint: //创建航点:

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

I expect that with my hasNext Function, if I pass an point like {1,1}, it will return true because there is one more point after this point. 我期望用我的hasNext函数,如果我传递像{1,1}这样的点,它将返回true,因为在这一点之后还有一点。 but when I pass{2,2}. 但是当我通过{2,2}时。 it will return false 它将返回false

Your method can be improved. 你的方法可以改进。 Take a look 看一看

boolean hasNext(){
    if (this.next != null) return true;
    return false;
}

You don't need to loop to the end of the LinkledList, you should only care about what is next. 您不需要循环到LinkledList的末尾,您应该只关心接下来的内容。

This line: 这一行:

assertEquals(true,elem.hasNext(createWaypoint(1, 1)));

Will always fail the assertion, because you're creating a new waypoint, and then attempting to find it in the list of existing waypoints. 将始终使断言失败,因为您正在创建新的航点,然后尝试在现有航点列表中找到它。 But this new waypoint isn't in the list . 但是这个新的航路点不在列表中

The only waypoints in the list are the ones that you added, and this is a brand new waypoint that just happens to have the same x and y values as one of the waypoints in your list. 列表中唯一的航点是您添加的航点,这是一个全新的航点,恰好具有与列表中某个航点相同的x和y值。

You don't include the code for the TourElement.addStart method, so I can't tell if there's any issue there. 你没有包含TourElement.addStart方法的代码,所以我不知道那里是否有任何问题。 And others have pointed out that you don't need a loop inside hasNext . 其他人指出你不需要在hasNext里面循环。 But the main issue here is that you need to do something other than use the WayPoint passed into the hasNext method. 但这里的主要问题是除了使用传入hasNext方法的WayPoint之外,还需要做一些事情。 This will likely involve traversing the waypoint graph trying to find an existing WayPoint with the same x and y values as the one passed into the method, and then checking to see if that has a next . 这可能会涉及遍历航点图试图找到一个现有的WayPoint有相同的x和y值作为一个传递到方法,然后检查是否一个next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定一个列表是否以相同的顺序包含在另一个列表中(在Java中)? - How can I determine whether one list is contained in another, in the same order (in Java)? 给定 Java InputStream,如何确定流中的当前偏移量? - Given a Java InputStream, how can I determine the current offset in the stream? 如何将不同的Java-Bean结构映射到另一个上 - How to map different Java-Bean structures onto one another 如何根据 Java 中的其他字符串列表减少我的结构列表? - how can I reduce my list of structures based on the other list of Strings in Java? 如何从Java中的列表中删除当前元素 - How to remove current element from List in java 用Java在另一个列表后面排序 - Sorting a list after another one in Java 如何在Java中验证日期(仅当前日期或之后) - How can i validate date (only current date or after) in Java 我如何确定某个特定的Java方法是否在几个堆栈级别下调用另一个特定的Java方法? - How can I determine if a particular Java method calls another particular Java method several stack levels down? 如何通过另一个 id 列表对 java 中的列表进行排序 - How can I sort a list in java by another list of ids java一个元素列表 - java one element list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM