Reputation: 14066
$tagArray = array(
"apples" => 12,
"oranges" => 38,
"pears" => 10,
"mangos" => 24,
"grapes" => 18,
"bananas" => 56,
"watermelons" => 80,
"lemons" => 12,
"limes" => 12,
"pineapples" => 15,
"strawberries" => 20,
"coconuts" => 43,
"cherries" => 20,
"raspberries" => 8,
"peaches" => 25
);
How I can do this in Java, and how to calling for the first and second params?
Upvotes: 3
Views: 825
Reputation: 421040
Java has no built in support for associative arrays. The corresponding datastructure in java is a Map
. In this case you could for instance make use of a HashMap
.
Here's one way.
Map<String, Integer> tagArray = new HashMap<String, Integer>() {{
put("apples", 12);
put("oranges", 38);
put("pears", 10);
put("mangos", 24);
put("grapes", 18);
put("bananas", 56);
put("watermelons", 80);
put("lemons", 12);
put("limes", 12);
put("pineapples", 15);
put("strawberries", 20);
put("coconuts", 43);
put("cherries", 20);
put("raspberries", 8);
put("peaches", 25);
}};
To get the value for, say, "lemons"
you do
int value = tagArray.get("lemons");
As @Peter Lawrey points out in the comments: If the order in the array is important to you, you could use a LinkedHashMap
instead of a HashMap
.
Upvotes: 13