Pregunta

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

¿Fue útil?

Solución

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);
    }
}

Otros consejos

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));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top