0

I'm taking Algorithms I on coursera and came across the implementation of a Data Structure called Bag.

http://algs4.cs.princeton.edu/13stacks/Bag.java.html

However I don't undertand why the use a Node class like this:

private static class Node<Item> { private Item item; private Node<Item> next; } 

Why is the Item in there? Why can't I use:

private class Node { private Item item; private Node next; } 

Is there a difference? Thanks

1
  • What does Item mean in the context of a non-generic world to you? I don't see anywhere that it could be introduced, or what it truly represents. What you've got there is a Node that holds Items, whereas the generic is a Node that can hold literally anything. Commented Feb 4, 2017 at 0:21

1 Answer 1

0

Yes, there is a difference.

Node<Item> uses what's called a generic. Item in not a class, it's simply an identifier of any element.

Read the Java source code, you would see something like Node<E>... Same thing. Name is not important.

For the other one, you need an Item class to be defined and that is the only datatype a Node can hold

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.