public class Node<E> {
E element;
Node<E> Next;
public Node(E e) {
element = e;
}
}
and then implemented the LinkedList using pointers. It was really quite fun.
But my lab partner does things differently than I do, and we didn't really come to an agreement as to the "right" way to do things. When we started implementing methods, he wanted invalid states to throw exceptions, where I wanted to return values that would indicate error states.
Like a getIndex(E e) method that returned -1 if e was not in the list, or a getValue(int index) method that returned null if the index was invalid (and then obviously check for getValue(index) == null or getIndex >= 0 before proceeding).
He wanted to throw NullPointerException or IndexOutOfBounds and wrap basically everything that you do with the LinkedList in a try/catch block. My understanding is that try/catch is kind of expensive.
In the end, we submitted an in-class lab assignment that used all this try/catch nonsense because I am quick to concede and doubt myself when faced with opposition and I'm not 100% confident, but when I extend it for my own assignment I'll refactor it. So what say you, wise developers of the TR forums? What's the "right" way to go about it?