Java finding a specific element

In summary, the programmer is having trouble inserting data into a linked list, and is unsure of how to debug the program.
  • #1
magnifik
360
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5


It seems like the issue may lie in the way you are setting the cursor to the head of the list. Depending on how your list is structured, the head may actually be at the end of the list, causing the data to be returned in reverse order. I would suggest checking your implementation of the doubly linked list and making sure that the head is set correctly. Additionally, you may want to consider using a different data structure or algorithm to retrieve the element at a specific index, such as a hash table or binary search. It's also important to thoroughly test your code and consider edge cases to ensure it is functioning correctly.
 

FAQ: Java finding a specific element

How do I find a specific element in a Java array?

To find a specific element in a Java array, you can use the indexOf() method. This method takes in the element you want to find as a parameter and returns the index of the first occurrence of that element in the array. If the element is not found, it will return -1.

Can I use a for loop to find a specific element in a Java array?

Yes, you can use a for loop to find a specific element in a Java array. Inside the for loop, you can use an if statement to check if the current element matches the element you are looking for. If it does, you can use the loop variable as the index of the element in the array.

Is there a built-in method to search for a specific element in a Java array?

Yes, there is a built-in method called Arrays.binarySearch() that can be used to search for a specific element in a sorted Java array. This method returns the index of the element if it is found, or a negative number if it is not found.

How can I find multiple occurrences of a specific element in a Java array?

To find multiple occurrences of a specific element in a Java array, you can use a for loop and an if statement to check for each occurrence. You can also use the Arrays.asList() method to convert the array to a list, and then use the contains() method to check for occurrences.

Can I use a lambda expression to find a specific element in a Java array?

Yes, you can use a lambda expression with the filter() method to find a specific element in a Java array. The filter() method takes in a predicate as a parameter, which can be a lambda expression that checks for the desired element. This method returns a stream of the matching elements, which can then be converted to an array using the toArray() method.

Similar threads

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