Results 1 to 6 of 6
  1. #1
    billq is offline Member
    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Default access class members with linked list

    Hello, I am not sure if my logic for implementing the following is correct. I have a linked list which works fine for primitive variables. I would like to store a list of a class called bullet which is
    Java Code:
    public class Bullet
    {
        int x;
        int y;
    
         //construtor to set value of x and y
    }
    The node class is
    Java Code:
    public class ListNode
    {
         Object bullet;
         ListNode  next;
         // constructors and other method.
    }
    In the body of the program, I am trying to print out the x y values with code
    ListNode current = firstNode;
    System.out.println("%s ", current.bullet) and I get the address of the bullet. I am not sure how to access the x value of the class. I hope I gave you enough info. If not please let me know and I'll post more code.
    thanks
    bill
    Last edited by Eranga; 05-09-2010 at 02:13 PM. Reason: added code tags

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    billq is offline Member
    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Default

    Yes, I was not sure if I should make the bullet class the node class or store a bunch of bullet classes in the linked list using the node class. I was hoping I could use the following code to print out x.
    System.out.println("%s ", current.bullet.x)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    As far as a ListNode is concerned its data member is just an Object; you know that you have stored a Bullet object in it so you should cast it back to a Bullet object:

    Java Code:
    ListNode node = ...;
    Bullet bullet= (Bullet)(ListNode.bullet);
    // you can access bullet.x and bullet.y here ...
    kind regards,

    Jos

  5. #5
    billq is offline Member
    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Default

    thanks Jos that worked

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by billq View Post
    thanks Jos that worked
    You're welcome; also have a look at the 'generics' mechanism; that way you can make your class a 'generic' class:

    Java Code:
    public class ListNode<T> {
       private T element;
       private ListNode<T> next;
       ...
    }
    You can build homogenous lists with that class, e.g a ListNode<Bullet> can only store objects of class Bullet as its data element. Have a look at it.

    kind regards,

    Jos

Similar Threads

  1. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  2. Linked List node class help!!
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 05-03-2010, 01:21 AM
  3. Need help using the Linked List class
    By wlc in forum New To Java
    Replies: 1
    Last Post: 08-13-2009, 03:38 AM
  4. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM
  5. Members List
    By Eranga in forum Suggestions & Feedback
    Replies: 11
    Last Post: 01-17-2008, 10:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •