the undo and redo implementation in Java
Asked Answered
S

4

3

I want to ask a question about List in Java.

It is easy to implement delete,add and search an element in a list. But how to implement the undo and redo of a list in Java?

can anyone help me on this?

Sewing answered 10/9, 2011 at 20:36 Comment(3)
What is "the undo and redo of a list"?Genniegennifer
for example, deleted a element, then i want to undo it, so I can add it backSewing
These are certainly not standard operations for a List class.Genniegennifer
E
4

You might be looking to implement a Command Design Pattern for this. A decent simplified example for List can be found here http://www.algosome.com/articles/implementing-undo-redo-java.html

Erminna answered 10/9, 2011 at 20:40 Comment(0)
P
1

I guess you want to undo and redo the operations like delete and add on a list.

Just use another list with indexing capabilities for Undo and Redo, e.g. an ArrayList:

  • Each time an add(element) or delete(element) to the original list really changes that list, you put the element at the end of your undo-list.
  • Then when you want to undo the operations, you just move through the undo-list: If the element in the undo-list is not in the original list, add it, if it is present in the original list, remove it.
  • If you want to use your undo-list for redo, too, then don't remove the elements you just "undid" from the undo-list, but rather move through the undo-list via an index. Then you can move through the undo-list in both direction and hence undo and redo your operations.
Police answered 10/9, 2011 at 20:51 Comment(0)
N
0

You mean like delete item from list and undo it? You can easily create new class for a list and define properties like: last performed action + store the origin value (and possibly index) of the effected item. The same for redo (at least for one step in redo and undo). If you don't care about order of the items (or you can order them easily), then define list of last performed actions and list of origin values. So for example: lastAction[0]="delete"; lastElement[0] = 1; // means you deleted 1 from the list

That's the first and dummy idea how to that. Perhaps there are some issues to consider...

Niche answered 10/9, 2011 at 20:47 Comment(0)
T
0

You need binding to do that.

To me this is the most efficient way to do that. This question at SO can give you some hints regarding from where to start.

Thromboplastin answered 10/9, 2011 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.