1

I would like to know how to create a structure to hold three lists in the following format:

[[[x],[y]],[z]]

with x,y,z being three lists of strings.

Note that I want to have the x and y lists on the 3rd level down and the z list on the 2nd level down.

I have tried creating 3 separate arraylists for x,y and z then another arraylist that holds x and y. The problem arise when I tried to create the outer most arraylist to hold all three, because something like

ArrayList<ArrayList<ArrayList<String>>> outerList = new ArrayList<ArrayList<ArrayList<String>>>();

would work for x and y but wont work for z because z is only 2 levels down.

I understand what I'm trying to do is more like a tree but I do not know how to implement a non-binary tree in java (x,y,z having more then just 2 branches).

What I'm trying to get out of this is a way to assign weights eg. user inputs a term, I iterate first through all the child nodes (x,y,z) and try to find it. If I find it I assign a high weight, otherwise I try to find it in the parent nodes (the list that holds x,y) and give it a moderate weight else I search in the root node and assign a minimum weight. The structure described is simply the relationship between the terms that I have categorized.

Any ideas on how to do this?

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
ariken929
  • 67
  • 2
  • 8

2 Answers2

2

This is where the power of objects comes in. You could create a class which contains the list of xs and ys, then create another class which holds a list of that class along with your list of xs. Finally you have a list of those classes.

If you want to get clever about it, try inheriting from ArrayList and overriding or overloading its methods to do what you need. http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

You also mentioned doing it in a tree manner. To do that there are some good suggestions here: Java tree data-structure?

Community
  • 1
  • 1
Dale Myers
  • 2,703
  • 3
  • 26
  • 48
1

The structure is easy enough to make, but working with it will be really ugly because you don't know how deep it goes. If this is a set structure, where you only ever have x, y, and z, you should make a custom object to handle that known structure. If you won't know the structure until runtime, then you need something like this.

Note that while this structure does match what you asked for, I don't necessarily recommend using it unless you have to. Look for alternatives. You mentioned a tree, but from your structure this would be a tree with data only at the leaf nodes, so keep that in mind if/when you implement it.

public void useArrayList()
{
    ArrayList<ArrayList<?>> weirdStructure = new ArrayList<ArrayList<?>>();
    ArrayList<String> x = new ArrayList<String>();
    ArrayList<String> y = new ArrayList<String>();
    ArrayList<String> z = new ArrayList<String>();          
    ArrayList<ArrayList<String>> xy = new ArrayList<ArrayList<String>>(); 
    xy.add(x);
    xy.add(y);
    weirdStructure.add(xy);     
    weirdStructure.add(z);

}
Thomas
  • 5,074
  • 1
  • 16
  • 12