3

I want to create an multidimensional array in Java, like the PHP one shown below. I want to create an exact same array. Is it possible?

Array
(
    [0] => Array
        (
            [name] => sagar
            [company] => Visa
        )

    [1] => Array
        (
            [name] => shloka
            [company] => Doctor
        )

    [2] => Array
        (
            [name] => sanket
            [company] => zon
        )

    [3] => Array
        (
            [name] => kamlesh
            [company] => nsc
        )

    [4] => Array
        (
            [name] => siddhi
            [company] => KES
        )

    [5] => Array
        (
            [name] => sanket
            [company] => zon
        )

    [6] => Array
        (
            [name] => bunty
            [company] => India Bull
        )

    [7] => Array
        (
            [name] => siddhi
            [company] => KES
        )

)

4 Answers 4

5

The "Way of the Java" would be to make a POJO class to store that info, like this:

class UserInfo {
    private String name;
    private String company;

    public UserInfo(String name, String company) {
        this.name = name;
        this.company = company;
    }

    public String getName() {
        return name;
    }

    public String getCompany() {
        return company;
    }
}

UserInfo[] users = {
    new UserInfo("sagar", "visa"),
    new UserInfo("shloka", "Doctor"),
    ....
};

However, if you want it exactly the same way you did in PHP, there you go:

public static Map<String,String> createUser(String name, String company) {
    final Map<String,String> result = new HashMap<String,String>();
    result.put("name",name);
    result.put("company",company);
    return result;
}

List<Map<String,String>> users = new ArrayList<Map<String,String>>();
users.add(createUser("sagar","visa"));
users.add(createUser("shloka","Doctor"));
....

Added:

For the sake of completeness of the answer, here's another way using instance initializers. It may look prettier that the other solutions in some cases, but basically it has one flaw: it uses unrequired permgen space. Not much of it, but it is still unrequired:

List<Map<String,String>> result = new ArrayList<Map<String,String>>() {{ 
    add(new HashMap<String,String>() {{
        put("name", "someName");
        put("company", "someCompany");
    }}); 
    add(new HashMap<String,String>() {{
        put("name", "someName1");
        put("company", "someCompany1");
    }}); 
}};

Basically this solution is overriding an ArrayList with a new class (that's why a permgen is used) which has an instance initializer with addition commands. Each addition also uses an overriden HashMap class with an instance initializer. This may be considered a "synthetic sugar" for Java.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for the addition, although it's somewhat on the baroque side. (Maybe we should call it "syntactic saccharine", since it leaves a bitter taste. :)) Just to pick a nit, though: you're using an instance initializer, not a static initializer.
@TedHopp Yes, you are right about that initializer, going to rename it.
4

In your case, you need an array of maps.

Something like:

List<Map<String, String>> list = new ArrayList<Map<String, String>>();
// 0
Map<String, String> tmp = new HashMap<String, String>();
tmp.put("name", "sagar");
tmp.put("company", "Visa");
list.add(tmp);
// 1
tmp = new HashMap<String, String>();
tmp.put("name", "shloka");
tmp.put("company", "Doctor");
list.add(tmp);
// so on, so forth...

Comments

1

Java does not have associative arrays; you can use a List of Maps instead.

List<Map<String, String>> myArray = new ArrayList<Map<String, String>>();
for (int i = 0; i < max; ++i) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("name", names[i]);
    map.put("company", companies[i]);
    myArray.add(map);
}

Unfortunately, there's no nice syntactic sugar that allows you to declare such a data structure without writing some procedural code.

1 Comment

Well, actually there is one kind of "synthetic sugar" in Java. But would create lots of unrequired permgen space (not that much actually, but still unrequired). I updated my answer in case you are curious.
0

Multi-dimensional arrays in Java are done as arrays of arrays.

int foo[][] = new int[8][8];

foo[0][0] = 3;

But yes, you want an array of Maps in your case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.