DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2005

    a linked list of different objects

    I'm having trouble putting different kinds of Objects in a Linked list,.

    I have this code:

    class Entry
    Key key;
    Object next; // can point to internal nodes, leaf nodes, or data objects

    public Entry(Key key, Object next)
    this.key = key;
    this.next = next;

    public void setNext(Object obj)
    this.next = obj;

    public void setKey(Key key)
    this.key = key;

    public String toString()
    return ("key: " + key.toString());
    // accessors and mutators

    in the setNext method of this class, I'm trying to send a Object of type LeafNode, and Java isn't letting me, I thought that you could send any class and as long as you accept an Object in the parameters, it would work.

    This is the method used to send the LeafNode:

    public static Node createRootNode()
    Node root = new InternalNode();
    root.entries[0].setNext( new LeafNode());
    return root;

    And this is the error message I get:

    H:\74.215 - Object Orientation\Assignment 1\a1q2.java:171: setNext(Object) in Entry cannot be applied to (LeafNode)
    root.entries[0].setNext( new LeafNode());

  2. #2
    Join Date
    Jan 2005
    I think but i'm not sure that you have to use downcasting in somewhere

    try to change this line
    PHP Code:
    root.entries[0].setNext( new LeafNode()); 
    PHP Code:
    root.entries[0].setNext( (object) new LeafNode()); 
    try it and tell me the result>>>

    good luck

  3. #3
    Join Date
    Nov 2004
    Well, I have no problems stuffing whatever into
    the Entry class. I changed the key variable to a
    String, cause I don't know 'Key' is, but that has no
    impact on the compile check:

    public EntryTest() {
      Entry en=new Entry("lala",new Vector());
      en.setNext(new Integer(0));
    So if your LeafNode is not from some obscure and unknown java basement where Object is no longer the
    base class, then the problem remains as to what the
    public array entries contained in Node is, and what
    the relation between Node and LeafNode is.


    A parameter of type Object accepts any class, and
    'downCasting' is a new concept in java (at least it is for me)
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.