• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Linked List method questions

 
Megan Hart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I am in a Java class and we are writing a decryption/encryption program using our own implementation of a LinkedList of integers.

I am having some problems with two of my methods and would love some help if anyone out there can help.

The first is a method to find the index of a value within the list.



I keep getting an index of 28 which is the length of my list. I know that it has something to do with the location
of index++; and/or the return statements but I'm not sure how to fix it. I've tried to move the return statements around but I can't find
anything that works.

The second is a method that gets the value at a specified index.



Regardless of what index value I pass to this method I always get a return value of 0 and I really don't know why.

Any help would be greatly appreciated!!

 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most likely explanation is that the value of "data" is 0 for all your nodes, and that's not the value you search for. In your first method, I'd return -1 after the loop to emphasize the value was not found in your linked list. Right now when it doesn't find the data, it just returns the size of the list. Also data is an int integer, not an Integer integer, right? You wouldn't want to compare Integer integers with ==.

For debugging, I'd print out the value of data as you step through the list. Is it always 0 like I suspect it is?
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does ListNode look like, code wise.

I think you some implement the next operation as a method, e.g: current.next();

WP
 
Megan Hart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my ListNode implementation.

 
Megan Hart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:The most likely explanation is that the value of "data" is 0 for all your nodes, and that's not the value you search for. In your first method, I'd return -1 after the loop to emphasize the value was not found in your linked list. Right now when it doesn't find the data, it just returns the size of the list. Also data is an int integer, not an Integer integer, right? You wouldn't want to compare Integer integers with ==.

For debugging, I'd print out the value of data as you step through the list. Is it always 0 like I suspect it is?


I've added elements to the list then printed out the results of the 'get' method and it always returns 0 and the value at that index is never zero.
But the problems I'm having seem to be in methods that use the current.data command.

And yes, the data is an int integer.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Megan,

Look at your constructors, you NEVER set the data value.

You need something like: this.data = data;

WP
 
Megan Hart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:Hello Megan,

Look at your constructors, you NEVER set the data value.

You need something like: this.data = data;

WP


Such a simple mistake, and I looked over that tons of times and never saw it.

Thanks for the second look!
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem.

WP
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic