Results 1 to 4 of 4
- 08-27-2012, 02:10 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 2
- Rep Power
- 0
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<>();
}
}
- 08-27-2012, 03:26 PM #2
Re: Quick question to do with LinkedList exercise
Why do they call it rush hour when nothing moves? - Robin Williams
- 08-27-2012, 03:30 PM #3
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
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.
- 08-27-2012, 06:35 PM #4
Member
- Join Date
- Aug 2012
- Posts
- 2
- Rep Power
- 0
Re: Quick question to do with LinkedList exercise
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
-
linkedlist question
By stuckonjava in forum New To JavaReplies: 2Last Post: 05-06-2012, 04:37 AM -
Anyone wanna take a shot at this quick exercise...
By JohnnyDickerson in forum New To JavaReplies: 7Last Post: 11-21-2011, 05:04 PM -
Anyone wanna take a shot at this quick exercise...
By JohnnyDickerson in forum Advanced JavaReplies: 3Last Post: 11-21-2011, 09:59 AM -
Making a method exercise question
By katiebear128 in forum New To JavaReplies: 6Last Post: 10-01-2011, 08:48 PM -
Quick LinkedList question
By mac in forum New To JavaReplies: 7Last Post: 05-31-2010, 01:27 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks