Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Collections - Linked list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Collections - Linked list" Watch "Collections - Linked list" New topic

Collections - Linked list

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok, here is my Number class....

can this be referenced as a node?...or element of a list?
because I went to the API and looked under list in the java.util,
and it said it had a add()method. but I read on further down
and it says its a boolean method.

well any ways. here is my main class.

now the List compiles fine, but when i try to do mylist.add(one);
, it doesn't want to compile, says it can't find the method add().

so do i need to make my own class and define an add method?

and also, in my book, it shows this as an example of a node.

So could i create a whole bunch of Node classes?...and add them to a List?

like for example..

class Node1
int info;
Node1 next;

but what i dont under stand is... what would Node1 point to?
which ever element is added next?

until I add one that is defined?
like so:

but when i declare it in a main class..
there is not public modifier...

so would i just do

Node3 node3 = new Node3();?


Node3 node3;
- and if i did do it like this, would the info be passed on?

Thanks for the help


You down with OOP? Yeah you know me!
Henry Wong

Joined: Sep 28, 2004
Posts: 20370

When you instantiate a List object, you are not instantiating a collection. Instead, you are instantiating your own class. The reason that the add() method doesn't exist, is because you never created one.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
M. Bunyard

Joined: Mar 28, 2006
Posts: 3

From the imported java.util.* package, it appears that you are trying to instantiate a java.util.List object. A java.util.List object cannot be instantiated due to the fact that it is an interface, but you can do the following if the Collections List is what your after:

The "java.util" is pre-pended when defining the mylist variable to remove the ambiguity in which List class (or interface) to use (either predefined java.util.List interface or your List class).
I agree. Here's the link:
subject: Collections - Linked list
It's not a secret anymore!