0

Short Version

How do you add values to a 3 dimensional array list at one go?

List<List<List<String>>> FD = new ArrayList<List<List<String>>>();

I learnt that for a normal Arraylist, the following can be done

List<String> R = new ArrayList<String>();
R.addAll(Arrays.asList("A","B","C","D","E"));

Longer Version

Note: I'll be using '->' symbol to represent 'determines'.
Read A -> B as 'A determines B'.

I am trying to capture the following information in some way.
A -> B, A,B -> C, B -> D,E

I felt a 3D list would come in handy as I pictured it in the following form

F[             --> Outermost List starts
  [            --> Middle List start (first of the three middle lists)
   [A],
   [B]
  ],            --> Middle List end
  [
   [A,B],        --> Innermost list start and end (first of the two inner lists in this middle list)
   [C]
  ],
  [
   [B],
   [D,E]
  ]
 ]             --> Outermost List ends

I chose a List since the size is dynamic to some extent (except that every middle list will always have only 2 inner lists)

I would appreciate if you could show me a simple way of populating such a list.

If you have some alternate implementation suggestions, I'm open to that too.

Thanks in advance

42cornflakes
  • 193
  • 4
  • 15
  • why in one row? its java, not bash script – Maxim Shoustin Oct 25 '13 at 18:54
  • You might be better of using a map instead of 3 dimension list – Shervin Asgari Oct 25 '13 at 18:56
  • @Maxim : Is there a more efficient way I can successfully capture the information I want to in Java? I will need to store it in some form or the other to be able to work on it. – 42cornflakes Oct 25 '13 at 18:57
  • Are you sure this is the most effective data structure? Does it make sense for some of these lists to be your own classes? – Zong Oct 25 '13 at 18:58
  • @Shervin: Thanks for the suggestion. Could you elaborate on the advantage I might get from that? – 42cornflakes Oct 25 '13 at 19:01
  • @Zong: This is the only thing I could think of. I'm welcome to other methods. Just that I might not be aware of it and might need a helping hand understanding it. Or would you suggest creating a class for capturing this and making it cleaner? – 42cornflakes Oct 25 '13 at 19:03
  • I'm asking if there is any hierarchy for these lists that make classes make sense. Eg. Top level is countries, inner level is cities, etc. – Zong Oct 25 '13 at 19:05
  • Lets call A->B as a dependency with A the determinant and B the dependent. Outer list has the list of all dependencies. Middle list captures the information of a single dependency. The 2 inner lists capture the list of determinants and dependents respectively. – 42cornflakes Oct 25 '13 at 19:12
  • Its difficult to say without knowing the complete picture of your objects, but if you can have an object act as a key, then this is much faster, easier and better solution – Shervin Asgari Oct 25 '13 at 19:20
  • @Shervin: Unfortunately, the determinant as well as the dependent (elaborated in the previous comment) can have duplication. Hence the predicament. – 42cornflakes Oct 25 '13 at 19:24
  • If duplication in the key, you can always use multimap from google guava – Shervin Asgari Oct 27 '13 at 14:36

2 Answers2

0

Like this. I copied Lists.newArrayList from Guava library.

import java.util.ArrayList;
import java.util.Collections;

public class Test {
    public static <E> ArrayList<E> newArrayList(E... elements) {
        ArrayList<E> list = new ArrayList<>(elements.length);
        Collections.addAll(list, elements);
        return list;
    }

    public static void main(String[] args) {

        ArrayList<ArrayList<ArrayList<String>>> lists = newArrayList(
            newArrayList(
                newArrayList("A"),
                newArrayList("B")
            ),
            newArrayList(
                newArrayList("A", "B"),
                newArrayList("C")
            ),
            newArrayList(
                newArrayList("B", "D"),
                newArrayList("E")
            )
        );

        System.out.println(lists);
    }
}
Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
  • Thanks a lot. The answer I was looking for. Will keep it open for some time just in case somebody offers alternate design suggestions. – 42cornflakes Oct 25 '13 at 19:22
0

This isn't strictly answering your question (the answer doesn't involve lists), but instead an alternate approach. In your question, you have some coordinate to represent each String value. Perhaps using a Map would be best.

Class to define the key:

public class Coordinate {
    private int x, y, z;

    // Static factory to make your code read a little better
    public static Coordinate create(int x, int y, int z) {
        return new Coordinate(x, y, z);
    }

    public Coordinate(int x, int y, int z) {
        // set this.x, this.y, this.z
    }

    public boolean equals(Object o) {
        // Compare this.x, y, z with o.x, y, z
    }

    public int hashCode() {
        // I'm sure you can come up with something
    }
}

Now, you could do something like this:

Map<Coordinate, String> map = new HashMap<Coordinate, String>();
map.put(Coordinate.create(1, 2, 3), "Some value");

// Prints out "Some value"
System.out.println(map.get(Coordinate.create(1, 2, 3));
bstempi
  • 2,023
  • 1
  • 15
  • 27