2012-01-14 16 views

回答

66

您可以使用HashMap中這樣

Map <String,String> map = new HashMap<String,String>(); 
//add items 
map.put("3433fd","Siva"); 
//get items 

String employeeName =(String) map.get("3433fd"); 
+1

感謝您的回覆。 – 2012-01-14 09:04:38

+0

是你在找什麼? – confucius 2012-01-14 09:05:10

+0

這些也是像(iPhone詞典) – 2012-01-14 09:07:45

9

您可以使用Bundle

因爲它提供了各種類型的映射的字符串。

Bundle b = new Bundle(); 
b.putInt("identifier", 121); 
b.putString("identifier", "Any String"); 
b.putStringArray("identifier", stringArray); 

int i = b.getInt("identifier"); 
... 
+0

謝謝。很多答案指向哈希映射,但實際上我認爲包更像字典,因爲它允許更多類型的變量 – Shayno 2017-07-30 14:35:02

5
public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     EditText textview = (EditText) findViewById(R.id.editTextHash); 
     EditText textviewNew = (EditText) findViewById(R.id.editTextHash2); 

     Map<String, String> map = new HashMap<String,String>(); 
     map.put("iOS", "100"); 
     map.put("Android", "101"); 
     map.put("Java", "102"); 
     map.put(".Net", "103"); 

     String TextString = ""; 
     // Set<E> keys = map.keySet(); 

     Set keys = map.keySet(); 
     System.out.println("keys "+keys); 
     for (Iterator i = keys.iterator(); i.hasNext();) 
     { 
      String key = (String) i.next(); 
      System.out.println("key "+key); 
      String value = (String) map.get(key); 
      System.out.println("value "+value); 
      textview.append(key + " = " + value); 
      TextString+=(key + " = " + value); 
     } 
     //textviewNew.setText(TextString); 

//  Iterator iterator = map.keySet().iterator(); 
//   
//  while (iterator.hasNext()) 
//  { 
//   String object = (String) iterator.next(); 
//   textview.setText(object); 
//  } 
//   
//  //textview.setText(map.get("Siva")); 
//   
//  System.out.println(map); 

    } 
} 
0

一個Dictionary是一個抽象類,鍵映射到值並沒有字典類android系統是指link你可以在類概述發現一張紙條

不要使用這個類,因爲它已經過時了。請使用地圖 界面進行新的實施。

,則嘗試插入使用一個null鍵或空值的字典將導致一個NullPointerException,但你可以使用Map接口,它提供了一個dictionary.you完全相同的功能,可以使用它作爲波紋管

//put values 
Map Message = new HashMap(); 
Message.put("title", "Test message"); 
Message.put("description", "message description"); 
Message.put("email", "[email protected]"); 
//get values 
Log.d("get Message","Message title -"+Message.get("title")); 

你也可以使用自定義類,如下

public class MessageObject { 
    String title; 
    String description; 
    String email; 
} 

你可以使用getter和setter獲取和放置值,不需要記住鍵名前夕您可以通過這種方式獲得其他優勢。

相關問題