2

I have never had to build an array in android like the following, and I think there are errors in my syntax.

I am trying to accomplish somethign like:

String RoomName[] = {"145"="PentHouse","125"="BoardRoom","45"="ShopRoom","8"="MainOffice"};

so If I reference something like:

String CurrentRoom = RoomName["8"];

I want CurrentRoom to be equal to MainOffice.

I know I am missing something obvious.

4 Answers 4

10

You don't want an array, you want a map:

Map<String, String> map = new HashMap<String, String>();
map.put("145", "PentHouse");
map.put("125", "BoardRoom");
map.put("145", "PentHouse");
// etc

String room = map.get("8");
// room will be null if "8" isn't a key in the map.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks your example is exactly what I am trying to accomplish +1 and will check shortly.
3

Java don't have associative arays, use HashMap instead

http://developer.android.com/reference/java/util/HashMap.html

HashMap<String, String> HM = new HashMap<String, String>();
HM.put("key1","value1");
HM.put("key2","value2");

Comments

2

This looks like you want a Map, not an array.

You can look at the examples in HashMap.

Comments

0

Java doesn't store arrays like that, what you're looking for is a Map<String, String> to hold key/value pairs.

For instance:

Map<String, String> myMap = new HashMap<String, String>();
myMap.put("145", "Penthouse");
...
String penthouse = myMap.get("145");  // will return "Penthouse"    

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.