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?
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
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.
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...
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.
Related
So the title does a terrible job at explaining what I mean, so let me explain.
So I basically want to make a mini cryptocurrency blockchain as a project. I'm trying to think of the best thing to use as the blocks in a blockchain. I'd need the blocks to hold the header information which is the following:
Previous Blocks Hash. Timestamp. Difficulty Target. Nonce. Version. Merkle Root.
And then the transactions that are also contained within each block. All in all the block would end up looking like this.
I thought a Hashmap would be the best way to do this as you can assign each of the above keys a value, but I'm not sure how I'd go about it. I would need a list that held the Hashmaps which held multiple keys and values, but I'd also need the list to hold whatever the transactions would be held in. I'd also need the next list to be able to see the previous list (so it can get the hash from everything in its header).
Could anyone give me some advice on the best way to deal with this? Been thinking it over for the last few days but I don't think I'm coming any closer to an answer.
Thank you.
So for a project for Uni I have a LinkedList with methods such as insert, delete, sort, reverse, etc. Part of the assignment is the implementation of an Undo button using Stacks.
is to have it so that every time i call a method, i push it's counter. For example I insert something, so i push a delete of that something. However I'm not sure if you can do that with a stack? If I understand stacks correctly aren't they primitive data types in an Array?
Sorry if the answer is obvious, but I really did do research on this and am still quite blank on the answer so any help is super appreciated!
This is a stack. Your assingment seems to follow the Command Pattern. So you would "record" each action (like add, delete, ...) in a LIFO (Last In - First Out)-stack. When you undo an action, you simply "reverse" the topmost command of the stack. How to "reverse" the operation exactly depends on the operation in question and is for you to figure out.
I think you are missing some aspects of a stack data structure.
A stack is a last in, first out. What this means is that you have some root Node that contains whatever the data type you need to hold is, and then a reference to the next Node. So for the last in, you would go to the very last Node in your linked list, and add it at the end. For removing, you would find the last Node, and remove the reference to it from the previous.
I don't think you've done enough research then, read up on linked lists, and the data structure stack. If you have any more questions, please ask, but try to find the answers yourself first.
I am very new to lotus notes. Recently my team mates were facing a problem regarding the Duplicates in Lotus notes as shown below in the CASE A and CASE B.
So we bought a app named scanEZ (Link About scanEX). Using this tool we can remove the first occurrence or the second occurrence. As in the case A and Case B the second items are considered as redundant because they do not have child. So we can remove all the second item as given below and thus removing the duplicates.
But in the Case 3 the order gets changed, the child item comes first and the Parent items comes second so i am unable to use the scanEX app.
Is there any other better way or software or script to accomplish my task. As I am new to this field I have not idea. Kindly help me.
Thanks in advance.
Probably the easiest way to approach this would be to force the view to always display documents with children first. That way the tool you have purchased will behave consistently for you. You would do this by adding a hidden sorted column to the right of the column that that you have circled. The formula in this column would be #DocChildren, and the sort options for the column would be set to 'Descending'. (Note that if you are uncomfortable making changes in this view, you can make a copy of it, make your changes in the copy, and run ScanEZ against the copy as well. You can also do all of this in a local replica of the database, and only replicate it back to the server when you are satisified that you have the right results.)
The other way would be to write your own code in LotusScript or Java, using the Notes classes. There are many different ways that you could write that code,
I agree with Richard's answer. If you want more details on how to go thru the document collection you could isolate the documents into a view that shows only the duplicates. Then write an agent to look at the UNID of the document, date modified and other such data elements to insure that you are getting the last updated document. I would add a field to the document as in FLAG='keep'. Then delete documents that don't have your flag in the document with a second agent. If you take this approach you can often use the same agents in other databases.
Since you are new to Notes keep in mind that Notes is a document database. There are several different conflicts like save conflicts or replication conflicts. Also you need to look at database settings on how duplicates can be handled. I would read up on these topics just so you can explain it to your co-workers/project manager.
Eventually in your heavily travelled databases you might be able to automate this process after you work down the source of the duplicates.
These are clearly not duplicates.
The definition of duplicate is that they are identical and so it does not matter which one is kept and which one is removed. To you, the fact that one has children makes it more important, which means that they are not pure duplicates.
What you have not stated is what you want to do if multiple documents with similar dates/subjects have children (a case D if you will).
To me this appears as three separate problems.
The first problem is to sort out the cases where more than one
document in a set has children.
Then sort out the cases where only one document in a set has children.
Then sort out the cases where none of the documents in a set has children.
The approach in each case will be different. The article from Ytira only really covers the last of these cases.
I'm building a database using a BST (binary search tree) and I want the user to be able to roll back the last 5 commands. Any Suggestions? I'm using Java.
Have you considered using Berkey DB? It's free and supported nested transactions (which would allow you to have any number of levels of rollback):
http://download.oracle.com/docs/cd/E17076_02/html/gsg_txn/JAVA/nestedtxn.html
Even if you decide to implement your own DB, it might be useful as a reference.
It sounds like you want the Memento pattern. Essentially, you create an object that has all of the information required to:
From the state of the tree before the operation, repeat the operation. (Redo)
From the state of the tree after the operation, revert the operation. (Undo)
You'd keep the last five of these around. When the user asks for an undo, take the latest, ask it to revert the operation, then indicate somehow (some index variable, for example) where you are in the list of mementos. You should then be able to move through the list in either direction, undoing and redoing as much as you want.
I'm writing a Java Tree in which tree nodes could have children that take a long time to compute (in this case, it's a file system, where there may be network timeouts that prevent getting a list of files from an attached drive).
The problem I'm finding is this:
getChildCount() is called before the user specifically requests opening a particular branch of the tree. I believe this is done so the JTree knows whether to show a + icon next to the node.
An accurate count of children from getChildCount() would need to perform the potentially expensive operation
If I fake the value of getChildCount(), the tree only allocates space for that many child nodes before asking for an enumeration of the children. (If I return '1', I'll only see 1 child listed, despite that there are more)
The enumeration of the children can be expensive and time-consuming, I'm okay with that. But I'm not okay with getChildCount() needing to know the exact number of children.
Any way I can work around this?
Added: The other problem is that if one of the nodes represents a floppy drive (how archaic!), the drive will be polled before the user asks for its files; if there's no disk in the drive, this results in a system error.
Update: Unfortunately, implementing the TreeWillExpand listener isn't the solution. That can allow you to veto an expansion, but the number of nodes shown is still restricted by the value returned by TreeNode.getChildCount().
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#data
scroll a little down, there is the exact tutorial on how to create lazy loading nodes for the jtree, complete with examples and documentation
I'm not sure if it's entirely applicable, but I recently worked around problems with a slow tree by pre-computing the answers to methods that would normally require going through the list of children. I only recompute them when children are added or removed or updated. In my case, some of the methods would have had to go recursively down the tree to figure out things like 'how many bytes are stored' for each node.
If you need a lot of access to a particular feature of your data structure that is expensive to compute, it may make sense to pre-compute it.
In the case of TreeNodes, this means that your TreeNodes would have to store their Child count. To explain it a bit more in detail: when you create a node n0 this node has a childcount (cc) of 0. When you add a node n1 as a child of this one, you n1.cc + cc++.
The tricky bit is the remove operation. You have to keep backlinks to parents and go up the hierarchy to subtract the cc of your current node.
In case you just want to have the a hasChildren feature for your nodes or override getChildCount, a boolean might be enough and would not force you to go up the whole hierarchy in case of removal. Or you could remove the backlinks and just say that you lose precision on remove operations. The TreeNode interface actually doesn't force you to provide a remove operation, but you probably want one anyway.
Well, that's the deal. In order to come up with precomputed precise values, you will have to keep backlinks of some sorts. If you don't you'd better call your method hasHadChildren or the more amusing isVirgin.
There are a few parts to the solution:
Like Lorenzo Boccaccia said, use the TreeWillExpandListener
Also, need to call nodesWereInserted on the tree, so the proper number of nodes will be displayed. See this code
I have determined that if you don't know the child count, TreeNode.getChildCount() needs to return at least 1 (it can't return 0)