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
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.
2 Comments
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
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
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.