• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections - Linked list

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();?

or

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

Thanks for the help

-Justin-
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

Henry
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic