The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

reflist (linkedlist)

Hey all i am trying to figure out how to get this RefList working for me...

Implement a method countValue() that counts the number of times an item occurs in a linked list.

int countValue(RefList f, int item);

Generate 20 random numbers in the range of 0 to 4, and insert each number at the front of a linked list. Output the list by using a method which you would call writeLinkedList which you would add to the RefList.java program. In a loop, call the method countValue() , and display the number of occurrences of each value from 0 to 4 in the list.

If not...well, either way, why are you adding new variables when variables that are there to do what you're doing are already present? (eg: head and current, where are those coming from? They're not in the RefList class, did you add them some where you don't show?)

"While the current node's next value is not null, set the current node's next value to not null" Are you sure this is what you want?

Style notes:
- comments for methods come before the method definition
- don't be lazy, always use braces to visually define your code sections (if, while, for, etc), you may need more than one line later and the time savings in time not spent figuring out why the line you just added always executes is well worth it.

public interface ListInterface
{
int size();
// Returns the number of elements on this list.
boolean contains (Object element);
// Returns true if this list contains an element e such that
// e.equals(element); otherwise, returns false.
boolean remove (Object element);
// Removes an element e from this list such that e.equals(element)
// and returns true; if no such element exists, returns false.
Object get(Object element);
// Returns an element e from this list such that e.equals(element);
// if no such element exists, returns null.
String toString();
// Returns a nicely formatted string that represents this list.
void reset();
// Initializes current position for an iteration through this list,
// to the first element on this list.
Object getNext();
public boolean add(Object obj);
// Preconditions: The list is not empty
// The list has been reset
// The list has not been modified since the most recent reset
//
// Returns the element at the current position on this list.
// If the current position is the last element, then it advances the value
// of the current position to the first element; otherwise, it advances
// the value of the current position to the next element.
}

Well from the code the "add" only adds whatever is placed into the list and does not need anything returned from that. So i figure the return would be void... But thats just with me guessing and looking at what the code "should" be doing.

I set them all to void and it compiled... it seems to be working now..

The method signature (I think) is made up of all the access modifiers, the return type, the method name, and all the methods parameters.

Yes and no, specifically the return type. (the access modifier isn't really part of the signature at all, but if you change it, it may only be less restrictive than the parent class' modifier for that method)

Yes, the return type is part of the signature (sort of...). No, changing it won't override a super class' method, nor will it overload a method in the same class - in fact, it won't compile in either case.

Why? Because Java cannot determine which method you want to use based only on the type you are assigning the return value to (or not assigning to, in the cases of a void return value or an unassigned return value).