1つのmapにおける複数のkeyの方法および種々の遍歴方法の例
2610 ワード
ListView学習における問題を解くことができます.、
結果は次のとおりです.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MapText {
public static void main(String [] args ){
String[] names = new String []{"android 1.1","android 2.1","android 3.1","android 4.1"};
int [] imgs = new int []{1,2,3,4};
String []infos =new String [] { "1991","1992","1993", "1994"};
List<Map<String ,Object>>listItem = new ArrayList <Map<String ,Object >>();
for (int i=0;i< names.length;i++){
Map<String, Object> map = new HashMap<String ,Object >();
map.put("name", names[i]);
map.put("img",imgs[i] );
map.put("info", infos[i]);
listItem.add(map);
}
for (Iterator iter =listItem.iterator() ; iter.hasNext();){
System.out.println(iter.next());
}
System.out.println("##Iterator iter =listItem.iterator() ; iter.hasNext();");
for (Iterator<Map<String, Object>> iter =listItem.iterator() ; iter.hasNext();){
System.out.println(iter.next());
}
System.out.println("##Iterator<Map<String, Object>> iter =listItem.iterator() ; iter.hasNext();");
for ( int i=0;i<listItem.size();i++){
System.out.println(listItem.get(i));
}
System.out.println("##int i=0;i<listItem.size();i++");
for (Map<String ,Object> list: listItem){
System.out.println(list);
}
System.out.println("##Map<String ,Object> list: listItem");
for (int i=0;i<listItem.size();i++){
System.out.println(listItem.get(i).get("info"));
}
}
}
結果は次のとおりです.
{name=android 1.1, img=1, info=1991}
{name=android 2.1, img=2, info=1992}
{name=android 3.1, img=3, info=1993}
{name=android 4.1, img=4, info=1994}
##Iterator iter =listItem.iterator() ; iter.hasNext();
{name=android 1.1, img=1, info=1991}
{name=android 2.1, img=2, info=1992}
{name=android 3.1, img=3, info=1993}
{name=android 4.1, img=4, info=1994}
##Iterator<Map<String, Object>> iter =listItem.iterator() ; iter.hasNext();
{name=android 1.1, img=1, info=1991}
{name=android 2.1, img=2, info=1992}
{name=android 3.1, img=3, info=1993}
{name=android 4.1, img=4, info=1994}
##int i=0;i<listItem.size();i++
{name=android 1.1, img=1, info=1991}
{name=android 2.1, img=2, info=1992}
{name=android 3.1, img=3, info=1993}
{name=android 4.1, img=4, info=1994}
##Map<String ,Object> list: listItem
1991
1992
1993
1994