Multidimensional array in Java like PHP
I want to create a multidimensional array in Java like PHP shown below. I want to create exactly the 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
)
)
source to share
In your case, you want an array of cards.
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...
source to share
The "Java way" would have to make a POJO class to store this information, for example:
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 as you did in PHP, 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 completeness of the answer, here's another way of using instance initializers. It may seem prettier that other solutions in some cases, but basically it has one drawback: it uses the raw permg space. Not much, but still not needed:
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 ArrayList with a new class (that's why a vergent is used) that has an instance initializer with additional commands. Each addon also uses an overridden HashMap class with an instance initializer. It can be considered synthetic sugar for Java.
source to share
Java has no associative arrays; you can use List
Map
.
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 is no strong syntactic sugar that allows you to declare such a data structure without writing procedural code.
source to share