How can I get an element from linked list with given index in java?

Nguyen Ngoc Hai :

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

Dmytro Patserkovskyi :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

get minimum element from linked list java

How can I get a first element from a sorted list?

How to insert node at given index of a linked list

How can I get the penultimate element in a list?

How can I remove an element from a list?

How to get the first element from a linked list in Oracle?

How can I get an element of a particular index from a set in GAMS?

Can I get the index of an element from inside the element? array

How can I get index of sorted list where element changes?

How can I write out an element from a list based on the index of an another element from an another list?

My problem is with linked list in Java? How can I use the elements (rabat) from linked list to achieve average price?

How can I get an index of an outer list element in a list of lists?

How can I conditionally remove elements from level 1 of a nested list given the value of a level 2 element?

How can I get the index of an element in a matrix from a list that is generated from that matrix?

How can I delete an element from a list when I do not know it's index number

How can I get element from list in JSTL (not forEach)?

How can I initialize a Doubly Linked List and then add the first element in java?

How I can get formated string from List array in Java?

How i can get GameObject index in the list?

How do I get get any element from a given link?

How can I remove this element from the list?

How can I add new element with given index into Linked-List?

How can I get Index of any element in python numpy array that has list inside?

How can I get a dictionary that contains a word from a sentence in a given list as key and list of it's occurences in that given list

How can I get the index of max values in list and then print the values from another list with max's index?

Removing element from Linked List by index

How can I get the index of a specific node in a doubly linked list?

How can I remove an element from a list by index, when the index matches a value from another list?

How can i get chemical element list?