「Java/連想配列」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「keyとvalueの関係で値を格納 ==値が文字列の場合== HashMap<<nowiki />String,String> content = new HashMap<<nowiki />String,String>(); content.put("id", "...」) |
(→連想配列ループ) |
||
| (同じ利用者による、間の1版が非表示) | |||
| 行27: | 行27: | ||
Log.d(TAG, cate.get("name")); | Log.d(TAG, cate.get("name")); | ||
} | } | ||
| + | |||
| + | ==連想配列ループ== | ||
| + | 文字列の場合 | ||
| + | for (Map.Entry<Integer, String> entry : map.entrySet()) { | ||
| + | String str1 = entry.getValue(); | ||
| + | } | ||
| + | 数字の場合 | ||
| + | for (Map.Entry<Integer, Integer> entry : map.entrySet()) { | ||
| + | Integer number1 = entry.getValue(); | ||
| + | } | ||
| + | |||
| + | 参考:https://qiita.com/kei2100/items/0ce97733c92fdcb9c5a9 | ||
2018年12月13日 (木) 18:30時点における最新版
keyとvalueの関係で値を格納
値が文字列の場合
HashMap<String,String> content = new HashMap<String,String>();
content.put("id", "test");
content.put("text", "message");
content.get("id"); // "test"
値が数値の場合
HashMap<String,Integer> content = new HashMap<String,Integer>();
content.put("id", 100);
content.put("text", 200);
content.get("id"); // 100
keyとvalueのhashの配列
ArrayList<HashMap<String, String>> cates = new ArrayList<HashMap<String, String>>();
HashMap<String, String> cate = new HashMap<String, String>();
cate.put("name", "hoge");
cate.put("id", 1);
cates.add(cate);
HashMap<String, String> cate = new HashMap<String, String>();
cate.put("name", "piyo");
cate.put("id", 2);
cates.add(cate);
for (HashMap<String, String> cate : cates)) {
Log.d(TAG, cate.get("id"));
Log.d(TAG, cate.get("name"));
}
連想配列ループ
文字列の場合
for (Map.Entry<Integer, String> entry : map.entrySet()) {
String str1 = entry.getValue();
}
数字の場合
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
Integer number1 = entry.getValue();
}
