I have a program which has a structure like this.
Document which contains (up to 20)
Chapters which contain (up to 100)
Pages which contain (up to 20)
Elements
This structure is represented by JPanels in my program. Meaning this structure has to be visually represented, and I'd rather not make a whole complex of ArrayList (unless absolutely neccessary) since each JPanels have a ZOrder of component, and a getParent() method.
This structure is one-dimenisonal, meaning that the parent has an one-dimensional array (and when I say array, it's purely descriptive, I don't mean ArrayList or anything similar) of its children. Each individual element has an index which represents it's location in(on?) it's parent. Number of elements in a page, and pages in a chapter is inconsistent.
It's easy to get the child's index within it's parent, but what about it's grandparents?
Since the elements can be (and usually are) numbered, having a one numbered list per chapter, I'd have to know the index of element in the Chapter, so I can adjust the numbers when a new element is added to the list (it doesn't have to be added in the end).
This can be solved in two ways (that I know of, that is):
Have an ArrayList in each chapter that keeps all the elements. This would require me that, everytime I add a new element to any page, to add it to the chapter array too.
To accomplish that I'd have to go trough all the previous pages, add up all the elements on them and add index of the new element on the present page to that number, the result being the index of the new element in the chapter, and therfore, in the array. And do that each time I add a new element.
Recreate the arrayList each time I need to get the order of elements in the chapter. Which again means going trought each page and adding each element one after another until I reach the end of chapter. And I'd need it each time a new element is added.
So the question is, which of this two methods is better (more efficient memory or processor time wise)? Which is more in the spirit of Java and programming altogether? Is there a third option that I am unaware of??
Chapter example:
Page one {
1. something
2. more something
3. nothing
.
.
.
16. still nothing
}
Page two {
17. maybe something
18. nope, still nothing
.
.
.
21. giberish
}
etc.
The question is: Which way of doing it is better? If you have a better idea, you can tell me, but I want to know which way of the above two is better non the less.
You need to make a tree. For some reason, programmers want to flatten everything out into tabular structures. You are talking about a tree, you need to either use one or make one.
Sadly, there is nothing in the Java Collections for implementing Trees. You can make them fairly easily.
If you have things that are different contained in the tree, but that need to be treated similarly (as nodes), then do a simple implementation of the Composite Pattern. A good example is a filesystem tree: each node is either a Folder or a File. If you both have them implement an interface called FilesystemItem, then you can put them into their tree structure.
Since you are doing a Document, I would recommend Composite.
Related
I have a List<Object> and each Object has integer 'id' and integer 'depends' parameter. This list then goes for some processing such that the objects which are not dependent on anyone will do the processing first and then next group of objects go ahead.
I have implemented this using topology sort along with adjacency list which does the job beautifully, but I am being told to use tree (TreeModel) as the previous approach is difficult to understand. The independent objects will be at root level, the object depending on it will be its children and so on. So, all objects at root level will go first, then object at level 2 and then 3...
I am a little confused as to how I would proceed with the implementation. I am thinking I would start with List<TreeModel<Object>>, so each index represents a tree and then form the trees accordingly. And while parsing, I will parse all the root level first, then the 2nd level and then 3rd.. Though I think there is an elegant way.
Any hint/ help would be appreciated. Thanks.
I had a question about whether or not Java has its own data structure for what I am looking for. It is something like a combination of an array, linked list and a tree.
If it is not in Java, but exists already as a concept in computer science/other languages, that is also an acceptable answer so I can research it more and find out how to implement it myself.
Here is a picture to better illustrate what I am looking for. Excuse the lack of professionalism; I made it as best as I could:
I am looking for something that starts with several indexed starting elements, that eventually link to other elements and end in a convergence of sorts (one final element). In the end, each index has its corresponding starting element, which is linked all the way to the final converged element.
It should be the case that asking for unknownStructure[i] or something should grab an object that is a representation of the ith starting element linked all the way to the final converged element. (This thing to be grabbed is outlined in various bright colors in the picture).
It seems to me that you are looking for a directed Graph data structure.
You may need to use a list of graphs if needed.
See this page for algorithms and this for implementation.
There is no "name" for this that I know of, but an array of linked list nodes would work quite well for this.
Traditionally linked lists are separate and simply a row of items pointing to the next. However, there is no reason why certain linked list nodes cannot point to the same child node. After all, trees and linked lists are essentially created the same way in Java.
The only foreseeable problem would be if you want to traverse this "tree" back to the starting node in the array. (Which could still be achieved with multiple parent support.)
To implement your linked-list array, simply created a Node class as for a linked list and then created an array of these elements:
Node[] myTreeArray = new Node[];
Then simply fill this array with your "base" nodes and link them to their appropriate children (eventually leading the the "end" node, which has a child of null)
Is it possible to create a traversal in java using neo4j that keeps a state for the duration of the traversal?
For example, I need an Evaluator that is almost identical to toDepth(), except that the depth at the current node is based on another comparison. Say that you had a linked list with 20 items, and you wanted the 10th [valid] one, meaning that some of the items had a particular property flag excluding them from the count. So the final returned item might actually be the 12th in the Path.
The only efficient way I can think of doing this is being able to store some state variable that is accessible to each individual evaluation. Is that possible?
I understand that I could write my own custom traversing functions to do this, but it would be nice if I could build it into the Traversal Framework.
a while ago i wrote an editor for a navigation graph that represented the paths inside (and between) buildings. it was stored inside a Graph-class.
the edges, for example were stored in one collection per floor, plus one collection for the ones that were between floors.
to draw them (only the current floor) or save them to disk (all of them), i needed to get at them from the outside. for that i implemented methods like callWithAllEdges and callWithAllEdgesIn, the latter taking a parameter to specify a floor.
those methods took a functor (at least i called it that), that was then called with the edges.
this is what drawing the edges of one floor looked like:
graph.callWithAllEdgesIn(id, new Functor<Edge>() {
public void call(Edge e) {
drawEdge(g,e);
}
});
this is a bit long-winded, of course. might be a problem with java and not with my solution though, i dont know.
another way would have been to just make a method that puts references to all the needed edges into a new collection and then iterate over that, i guess. seemed kind of wrong to me though.
my question is: how could i have solved that better?
Your current design is perfectly reasonable. Another option is to provide an Iterable/Iterator. That way you don't need to copy everything into a new list, but can instead lazily step through your internal lists.
Hi
I want to make a binary tree which is based -level,which means that nodes of first level will be create and then all the nodes of second level will be creat and then all the nodes of third level will be create ,...
for making a binary tree like above ,I used a list(data structure)
I want to know that JVM can remember the order of nodes or I must use list?
thanks
You would need to use a data structure that maintains the links between your elements. If you just had an array and created everything at level 1, then created a second array at level 2, the JVM won't remember for you the order that things were created.
In short, you're likely going to want to use a Linked List for something like that.