user707549
user707549

Reputation:

the undo and redo implementation in Java

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?

Upvotes: 3

Views: 4261

Answers (4)

Ben
Ben

Reputation: 13615

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

Upvotes: 4

Kowser
Kowser

Reputation: 8261

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.

Upvotes: 0

DaveFar
DaveFar

Reputation: 7447

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.

Upvotes: 1

ladar
ladar

Reputation: 5876

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

Upvotes: 0

Related Questions