aspose file tools*
The moose likes Beginning Java and the fly likes Java Linked List method questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Linked List method questions" Watch "Java Linked List method questions" New topic
Author

Java Linked List method questions

Megan Hart
Greenhorn

Joined: Dec 08, 2010
Posts: 6
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

Joined: Oct 01, 2001
Posts: 2849
    
  11

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

Joined: Mar 28, 2012
Posts: 859

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

Joined: Dec 08, 2010
Posts: 6
This is my ListNode implementation.

Megan Hart
Greenhorn

Joined: Dec 08, 2010
Posts: 6
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

Joined: Mar 28, 2012
Posts: 859

Hello Megan,

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

You need something like: this.data = data;

WP
Megan Hart
Greenhorn

Joined: Dec 08, 2010
Posts: 6
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

Joined: Mar 28, 2012
Posts: 859

No problem.

WP
 
jQuery in Action, 2nd edition
 
subject: Java Linked List method questions