Comp Sci Java finding a specific element

AI Thread Summary
The discussion revolves around a Java implementation issue with a doubly linked list, where the user is retrieving elements in reverse order. The user is attempting to access the element at a specific index but is encountering unexpected results, suggesting a potential error in how elements are inserted into the list. Suggestions include verifying the order of insertion and implementing debugging techniques to trace the program's execution. Additionally, concerns are raised about the use of instance variable naming conventions and direct access to node properties without appropriate getters. The user acknowledges issues with garbage values during insertion and expresses uncertainty about effective debugging methods.
magnifik
Messages
350
Reaction score
0
i am using dummy nodes for a doubly linked list and trying to get an object at the specified index. for whatever reason, when i test it, i am getting data but it is backwards. for example, when i have a list of integers from 0 to 21, and i attempt to call get(2), i get 19 which is at index 2 starting at the back of the list. since i am setting the cursor to be at the head, i am not sure what is causing this.


public Object get(int index) {
if (index < 0 || index > size()-1) throw new IndexOutOfBoundsException();
Node cursor = _head._next;
for (int i = 0; i < index; i++)
cursor = cursor._next;
return cursor._data;
}
 
Physics news on Phys.org
It's hard to tell without seeing your whole program, but I put good odds on the explanation that you are inserting things into the list in the wrong order. (e.g. you keep inserting new elements in the front of the list)


Have you checked that the contents of the list really are what you expected? Have you put debugging statements into your code so that you can observe things throughout the entire execution of your program? (or otherwise used some means to trace your programs execution)

Remember that you aren't supposed to be learning just how to program -- you're also supposed to be learning how to debug a programs.
 
Is this method inside a class called something like "LinkedList"? And I'm guessing that this class has a variable for head and a variable for tail? If so, I see some problems.

First, why have you added an underscore before the names of the instance variables? Second, you should not be accessing the variable "_next" like you are. You should have a getter in Node for this variable. At any rate, I suggest you do what Hurkyl suggested.
 
Hurkyl said:
It's hard to tell without seeing your whole program, but I put good odds on the explanation that you are inserting things into the list in the wrong order. (e.g. you keep inserting new elements in the front of the list)


Have you checked that the contents of the list really are what you expected? Have you put debugging statements into your code so that you can observe things throughout the entire execution of your program? (or otherwise used some means to trace your programs execution)

Remember that you aren't supposed to be learning just how to program -- you're also supposed to be learning how to debug a programs.

i checked if the contents were correct, but when i try to add to my list, i am seeing garbage values rather than the integers that i intended to add to my list. as for debugging, i am unsure of how to do thisthoroughly. thanks for the suggestion.
 

Similar threads

Replies
1
Views
2K
Replies
9
Views
4K
Replies
11
Views
12K
Replies
3
Views
2K
Replies
12
Views
2K
Replies
4
Views
1K
Replies
2
Views
6K
Replies
2
Views
1K
Replies
1
Views
1K
Back
Top