This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections - Linked list  RSS feed

Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Henry Wong
Posts: 23260
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

M. Bunyard
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).
look! it's a bird! it's a plane! It's .... a teeny tiny ad
Thoughts on deprecation in Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!