Skip to content
Advertisement

3-Dimension List or Map

I need something like a 3-dimension (like a list or a map), which I fill with 2 Strings and an Integer within a loop. But, unfortunately I don’t know which data structure to use and how.

// something like a 3-dimensional myData
for (int i = 0; i < 10; i++) {
    myData.add("abc", "def", 123);
}

Advertisement

Answer

Create an object that encapsulates the three together and add them to an array or List:

public class Foo {
    private String s1;
    private String s2; 
    private int v3;
    // ctors, getters, etc.
}

List<Foo> foos = new ArrayList<Foo>();
for (int i = 0; i < 10; ++i) {
    foos.add(new Foo("abc", "def", 123);
}

If you want to insert into a database, write a DAO class:

public interface FooDao {
    void save(Foo foo);    
}

Implement as needed using JDBC.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement