Java 迭代映射的ArrayList的ArrayList
我使用SimpleExpandableListAdapter为我的应用程序创建ExpandableListView。我想知道如何更好地使用列表和地图,以及它们在实践中是什么Java 迭代映射的ArrayList的ArrayList,java,android,arraylist,Java,Android,Arraylist,我使用SimpleExpandableListAdapter为我的应用程序创建ExpandableListView。我想知道如何更好地使用列表和地图,以及它们在实践中是什么 //collection for elements of a single group; ArrayList<Map<String, String>> childDataItem; //general collection for collections of elements ArrayList
//collection for elements of a single group;
ArrayList<Map<String, String>> childDataItem;
//general collection for collections of elements
ArrayList<ArrayList<Map<String, String>>> childData;
Map<String, String> m;
//单个组元素的集合;
ArrayList childDataItem;
//元素集合的常规集合
ArrayList儿童数据;
地图m;
我知道如何迭代地图的ArrayList,这对我来说不是问题,但我被卡住了
childData = new ArrayList<>();
childDataItem = new ArrayList<>();
for (String phone : phonesHTC) {
m = new HashMap<>();
m.put("phoneName", phone);
childDataItem.add(m);
}
childData.add(childDataItem);
childDataItem = new ArrayList<>();
for (String phone : phonesSams) {
m = new HashMap<String, String>();
m.put("phoneName", phone);
childDataItem.add(m);
}
childData.add(childDataItem);
// создаем коллекцию элементов для третьей группы
childDataItem = new ArrayList<>();
for (String phone : phonesLG) {
m = new HashMap<String, String>();
m.put("phoneName", phone);
childDataItem.add(m);
}
childData.add(childDataItem);
childData=newarraylist();
childDataItem=新的ArrayList();
用于(字符串电话:phonesHTC){
m=新的HashMap();
m、 输入(“电话名”,phone);
childDataItem.add(m);
}
添加(childDataItem);
childDataItem=新的ArrayList();
用于(字符串电话:phonesSams){
m=新的HashMap();
m、 输入(“电话名”,phone);
childDataItem.add(m);
}
添加(childDataItem);
// создаем коллекцию элементов для третьей группы
childDataItem=新的ArrayList();
用于(字符串电话:phonesLG){
m=新的HashMap();
m、 输入(“电话名”,phone);
childDataItem.add(m);
}
添加(childDataItem);
我想记录childData包含的内容(
),因此您可能知道,数组列表只是数据对象的顺序存储。映射是一种键值对映射,其中键用作查找,并且必须是唯一的。也就是说,在映射中,您可能有许多重复值,但只有一个键
至于迭代映射
,您可以使用一个条目集,这会让它变得更简单。因此,如果您想迭代类型的对象,如果您想记录childData的所有元素,那么就不需要最后一个循环,您已经在第一个循环中获取了它们。请从程序中删除下面的代码,并将其删除将记录所有childData项
for (Map<String, String> innerEntry : childDataItem) {
for (String key : innerEntry.keySet()) {
String value = innerEntry.get(key);
Log.d("MyLogs", "(childDataItem)key = " + key);
Log.d("MyLogs", "(childDataItem)value = " + value);
}
}
输出
MyLogs (childData)value1 = HTC1
MyLogs (childData)key = phoneName1
MyLogs (childData)value1 = HTC
MyLogs (childData)key = phoneName
MyLogs (childData)value1 = Samsung
MyLogs (childData)key = phoneName
MyLogs (childData)value1 = LG
MyLogs (childData)key = phoneName
@AnimeKnight不客气。如果您认为答案有用,请向上投票。声誉低于15的人所投的票会被记录下来,但不会更改公开显示的帖子分数。:/还需要一分,我真的很抱歉sorry@AnimeKnight没问题,当你得到1分时,请投票:)
for(List<Map<String, String>> childDataItemList : childData){
for(Map<String, String> map : childDataItemList){
//Take each map we have in the list and iterate over the keys + values
for(Map.Entry<String, String> entry : map){
String key = entry.getKey(), value = entry.getValue();
}
}
}
for (Map<String, String> innerEntry : childDataItem) {
for (String key : innerEntry.keySet()) {
String value = innerEntry.get(key);
Log.d("MyLogs", "(childDataItem)key = " + key);
Log.d("MyLogs", "(childDataItem)value = " + value);
}
}
ArrayList<Map<String, String>> childDataItem;
//general collection for collections of elements
ArrayList<ArrayList<Map<String, String>>> childData;
Map<String, String> m;
childData = new ArrayList<>();
childDataItem = new ArrayList<>();
m = new HashMap<>();
m.put("phoneName", "HTC");
m.put("phoneName1", "HTC1");
childDataItem.add(m);
childData.add(childDataItem);
childDataItem = new ArrayList<>();
m = new HashMap<String, String>();
m.put("phoneName", "Samsung");
childDataItem.add(m);
childData.add(childDataItem);
// создаем коллекцию элементов для третьей группы
childDataItem = new ArrayList<>();
m = new HashMap<String, String>();
m.put("phoneName", "LG");
childDataItem.add(m);
childData.add(childDataItem);
for (ArrayList<Map<String, String>> outerEntry : childData) {
for(Map<String, String> i:outerEntry ) {
for (String key1 : i.keySet()) {
String value1 = i.get(key1);
System.out.println("MyLogs (childData)value1 = " + value1);
System.out.println("MyLogs (childData)key = " + key1);
}
}
}
MyLogs (childData)value1 = HTC1
MyLogs (childData)key = phoneName1
MyLogs (childData)value1 = HTC
MyLogs (childData)key = phoneName
MyLogs (childData)value1 = Samsung
MyLogs (childData)key = phoneName
MyLogs (childData)value1 = LG
MyLogs (childData)key = phoneName