Implementing LinkedList as a user?
Hello JPF! I have just been introduced to LinkedList and I am working on my first programming assignment, but I am really confused and lost. My professor wants us to implement the program as a user; I am unsure what she means, so can anyone please explain this? Because I have just been introduced to LL, I am just playing around with it, and so far, I haven't been able to get anything to work.
I'm not writing any particular program at the moment, but I haven't been able to get the code below to print out right; I've been getting an error because there's something wrong with my insert method, but I do not know what. Can someone please tell me what I'm doing wrong?
Code :
import java.util.LinkedList;
public class Editor
{
private LinkedList<String> text;
public String display()
{
return text.getFirst() + "\n" + text.getLast();
}
public void insert(String s)
{
text.add(s);
}
}
Code :
public class Tester
{
public static void main (String [] args)
{
Editor e = new Editor();
e.insert("test1");
e.insert("test2");
e.insert("test3");
System.out.println(e.display());
}
}
Re: Implementing LinkedList as a user?
Hello,
First thing I noticed when I tried to run your code was a nullpointerexception on the variable text.
You need to actually initialise/create the linked list.
Code :
private LinkedList<String> text = new LinkedList<String>();
After that, everything worked just fine for me. For more information on the LinkedList have a look at LinkedList (Java Platform SE 6)
// Json
Re: Implementing LinkedList as a user?
Re: Implementing LinkedList as a user?
If you are satisfied with the answer, please use the Thread Tools menu at the top to mark this thread as SOLVED.
Cheers!
// Json