Java finding a specific element

  • Context: Comp Sci 
  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Element Java Specific
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to a doubly linked list implementation in Java, specifically focusing on retrieving an object at a specified index. Participants explore potential reasons for unexpected behavior in the output when accessing list elements.

Discussion Character

  • Technical explanation
  • Debugging assistance
  • Conceptual clarification

Main Points Raised

  • One participant describes an issue where retrieving an element at a specific index returns data in reverse order, suggesting a problem with how elements are inserted into the list.
  • Another participant proposes that the issue may stem from inserting elements in the wrong order, particularly if new elements are added to the front of the list.
  • A third participant questions the structure of the class and the naming conventions used for instance variables, suggesting that accessing the "_next" variable directly may not be appropriate.
  • One participant mentions seeing garbage values when attempting to add integers to the list, indicating potential issues with memory management or initialization.
  • Several participants emphasize the importance of debugging techniques, suggesting the use of debugging statements to trace program execution.

Areas of Agreement / Disagreement

Participants express various hypotheses about the cause of the issue, but there is no consensus on the exact problem or solution. Multiple competing views on debugging and implementation strategies remain present.

Contextual Notes

Limitations include the lack of visibility into the entire program, which may hinder accurate diagnosis of the issue. The discussion also reflects uncertainty regarding proper debugging methods and the implications of variable naming conventions.

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 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
12K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K