Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help with ListIterator

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Help with ListIterator

    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?
    Last edited by vluong; October 16th, 2009 at 02:47 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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.

    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

  4. #4
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default 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?

  5. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default 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.

    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
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.