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

Grzegorz Górkiewicz
  • 4,496
  • 4
  • 22
  • 38
  • 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 `Item`s, whereas the generic is a `Node` that can hold literally *anything*. – Makoto Feb 04 '17 at 00:21

1 Answers1

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245