Results 1 to 4 of 4
  1. #1
    SAH101 is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Quick question to do with LinkedList exercise

    I am going through a book called java programming language;
    there's one particular exercise which has been puzzling me for a few days now.

    the question is:

    Write a LinkedList class that has a field of Object and a reference to the next LinkedList element in the list.

    because this question is near the beginning of the book and lists haven't been covered at this point, I just don't understand how to tackle this question.

    so far i've tried the following but im getting errors concerning the 5th and 9th line:

    package linkedlist;

    import java.util.*;
    public class LinkedList {
    Object obj = list1.nextElement();

    public static void main(String[]args) {
    String[] objt = {"objt1", "objt2", "objt3"};
    List<String> list1 = new LinkedList<>();

    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Quick question to do with LinkedList exercise

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Quick question to do with LinkedList exercise

    The error on line 5 is because you are instantiating an object. You must move that into the main-method.

    You could keep just the declaration of the object, but you can't instantiate it until later.

    What do you really want to accomplish on line 9? Check your equals sign and then both sides. Also, read the errors you are getting.

  4. #4
    SAH101 is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Re: Quick question to do with LinkedList exercise

    Quote Originally Posted by Zyril View Post
    The error on line 5 is because you are instantiating an object. You must move that into the main-method.

    You could keep just the declaration of the object, but you can't instantiate it until later.

    What do you really want to accomplish on line 9? Check your equals sign and then both sides. Also, read the errors you are getting.
    Ok so now i've made the Object field like the question asks (to make a method of type field) which looks like this:

    Object obj;

    which is outside of the main method.

    The question says that this field must have a reference to the next LinkedList element in the list. But how exactly would I do this list would I use an array, linkedlist? or would a suitable option be to make a static field for a list of type Object something like so:

    public static Object list1[] = {"first", "second"};

Similar Threads

  1. linkedlist question
    By stuckonjava in forum New To Java
    Replies: 2
    Last Post: 05-06-2012, 04:37 AM
  2. Anyone wanna take a shot at this quick exercise...
    By JohnnyDickerson in forum New To Java
    Replies: 7
    Last Post: 11-21-2011, 05:04 PM
  3. Anyone wanna take a shot at this quick exercise...
    By JohnnyDickerson in forum Advanced Java
    Replies: 3
    Last Post: 11-21-2011, 09:59 AM
  4. Making a method exercise question
    By katiebear128 in forum New To Java
    Replies: 6
    Last Post: 10-01-2011, 08:48 PM
  5. Quick LinkedList question
    By mac in forum New To Java
    Replies: 7
    Last Post: 05-31-2010, 01:27 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
  •