2

I'm learning about link-list in Java. and I'm want to write a method to give the value of node based on given index

I write a function, but it does not pass some of testcases, and I dont know why?. what is wrong with my logic?

//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;
 }
int getNoOfWaypoints()// return the number of waypoints in the list
{
 int count = 1;
 TourElement current = getNext();
 while(current.next != null)
 {
 count++;
 current = current.next;
 System.out.println(count);
 }
 return count;
} 

// here is method I'm strucking with:

Waypoint getWaypointAt(int index)
{
 int totalElement = getNoOfWaypoints();
 int count = 0;
 TourElement current = getNext();
 if(index < totalElement && index >= 0)
 {
 while (current.next != null)
 {
 if(count == index)
 {
 return getWaypoint();
 }
 count++;
 current = current.next;
 }
 }
 return null;
}

//the testcase: //case 1: pass

public void test0GetWaypointAt_First() {
 TourElement elem = createElementList(new int[][] {{0, 0}, {1, 1}, {2, 2}});
 Waypoint expected = createWaypoint(0, 0);
 assertArrayEquals(expected.toArray(), elem.getWaypointAt(0).toArray());
 }

//case 2 and case 3: failed

public void test0GetWaypointAt_Snd() {
 TourElement elem = createElementList(new int[][] {{0, 0}, {1, 1}, {2, 2}});
 Waypoint expected = createWaypoint(1, 1);
 assertArrayEquals(expected.toArray(), elem.getWaypointAt(1).toArray());
 }
 @Test
 public void test0GetWaypointAt_Last() {
 TourElement elem = createElementList(new int[][] {{0, 0}, {1, 1}, {2, 2}});
 Waypoint expected = createWaypoint(2, 2);
 assertArrayEquals(expected.toArray(), elem.getWaypointAt(2).toArray());
 }

I dont know the reason. Please help me. Thank you so much in advance

asked May 28, 2019 at 14:34
1

1 Answer 1

1

Structure you've build with list seems ok, about your method getWaypointAt I see at least two problems:

First problem

 TourElement current = getNext();

getNext() is already the next element in your list, so you're skipping the first element all the time. It should be

 TourElement current = this;

Second problem

 return getWaypoint();

It returns waypoint from the head all the time. Should be

 return current.getWaypoint();

Seems like your first test should fail as well. I don't see the way you build elements in createElementList, it could be a reason why it pass.

answered May 28, 2019 at 15:21
2
  • thank you so much. I though getNext() in the first time will be the first element in my list. But I was wong. Again, thank u so much for your help!!! Commented May 28, 2019 at 15:30
  • Happy to help :) Commented May 28, 2019 at 15:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.