LinkedList

13 views
Skip to first unread message

Caroline

unread,
Apr 23, 2011, 3:12:35 PM4/23/11
to UBC CPSC 210
I did the reading and exercise, but I don't understand the point of
having the inner class. Can you explain?

Paul Carter

unread,
Apr 23, 2011, 8:34:13 PM4/23/11
to ubc-cp...@googlegroups.com
The inner class represents a node in the linked list. Given that the node class is concerned exclusively with the implementation of the linked list and is therefore something that a client of the linked list needs to know nothing about, we make it a private inner class of the linked list class. By making the node class a private inner class of the linked list class, we also *prevent* a client from accessing it - this is a good thing - we don't want clients messing with nodes given how easy it is to break the linked list.

This is an example of data abstraction - the client is presented with public methods that allow items to be added/removed to/from the list, etc. But the client needs to know nothing about how the list is implemented in order to use the list effectively and is indeed prevented from accessing anything concerned with the implementation.

Does that help?

Paul

Caroline

unread,
Apr 24, 2011, 4:34:26 PM4/24/11
to UBC CPSC 210
Yeah, thanks :)

On Apr 23, 5:34 pm, Paul Carter <pcar...@cs.ubc.ca> wrote:
> The inner class represents a node in the linked list.  Given that the node class is concerned exclusively with the implementation of the linked list and is therefore something that a client of the linked list needs to know nothing about, we make it a private inner class of the linked list class.  By making the node class a private inner class of the linked list class, we also *prevent* a client from accessing it - this is a good thing - we don't want clients messing with nodes given how easy it is to break the linked list.  
>
> This is an example of data abstraction - the client is presented with public methods that allow items to be added/removed to/from the list, etc. But the client needs to know nothing about how the list is implemented in order to use the list effectively and is indeed prevented from accessing anything concerned with the implementation.
>
> Does that help?  
>
> Paul
>

Caroline

unread,
Apr 25, 2011, 5:13:29 PM4/25/11
to UBC CPSC 210
One more question - I'm still confused about why we use .equals() when
comparing Elements. I see in the tests we are using Integer, so that
makes sense, but shouldn't the list be able to store anything
including primitive types like int?

Paul Carter

unread,
Apr 25, 2011, 7:51:19 PM4/25/11
to ubc-cp...@googlegroups.com
A type parameter (i.e., E in ArrayList<E> or HashSet<E>,...) can represent reference types only, not primitive types (like int, double, char,...). So, the following is not allowed:

List<int> myList = new ArrayList<int>();

but this is:

List<Integer> myList = new ArrayList<Integer>();

Can you now see why we use .equalsI() and not ==?

Paul

Caroline

unread,
Apr 25, 2011, 9:35:46 PM4/25/11
to UBC CPSC 210
Oh, OK. Yeah - thanks!
Reply all
Reply to author
Forward
0 new messages