Re: Help with ListIterator
listIterator in Linked Lists
You can't modify a linked list with and without an iterator at the same time. Once you modify a linked list, you need a new iterator (this of course doesn't count modifying the linked list with the iterator).
Also, it's bad practice to initialize fields without a constructor.
Re: Help with ListIterator
I think you're adding an extra layer where which you dont really need.
Below is a version of your code without the Editor class which seems somewhat redundant.
The LinkedList is already an implementation of the List interface and there is no need in rewriting it.
Code :
public class Tester
{
public static void main (String [] args)
{
final List<String> text = LinkedList<String>();
text.add("I am Tom");
text.add("You are Jane");
text.add("This is a book");
// Here is the same thing as the test method on the Editor class
text.add("Test test test");
// Here is the same thing as the display method on the Editor class
for(final String string : text) {
System.out.println(string);
}
}
}
Hope this helps!
// Json
Undo Method for Text Editor
Thanks helloworld922 and Json.
The reason why I am trying to write an Editor class with ListIterator is because I am trying to implement a text editor program. I have figured it out for the most part, but I have stumbled across another problem.
I need to write a method to undo the preceding step. For example: I'll add "Hello" to the LinkedList and then decide to undo it. My idea is to clone the LinkedList and then if the user calls undo, I'll just assign the cloned copy to the LinkedList.
Does someone have a better idea?
Re: Help with ListIterator
If you have an undo history of say 5, you can just use text.remove() and assign the returned value to an array or ArrayList. Then if the user calls redo (ie put that value you pack) you could do ArrayList.remove(ArrayList.size()-1) and add that back onto your LinkedList.
Code :
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Linky {
private static ArrayList<String> history;
private static List<String> text;
public static void main(String[] args) {
history = new ArrayList<String>();
text = new LinkedList<String>();
append("hello");
append("hey");
append("Morning");
append("Afternooon");
System.out.println(text);
undo();
append("Afternoon");
System.out.println(text);
undo();
redo();
System.out.println(text);
}
public static void append(String s){
text.add(s);
}
public static void undo(){
history.add(text.remove(text.size()-1));
}
public static void redo(){
text.add(history.remove(0));
}
}
Maybe something like that? You will need to play around with the exact functionality of undo and redo buttons, but you get the idea. Since they are way out :P
Re: Help with ListIterator
Undo-redo seems more at-home to me implemented as a stack. Simply initialize two stacks, one for undo and one for redo, then every time the user does something, push it onto the undo stack. If the user uses undo, pop it off the undo stack and onto the redo stack, and if they undo again, reverse the process to get it back onto the undo stack. If the user does something other than undo/redo, clear the redo stack.