[英]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.