Java的找回返回的ArrayList到HashMap类 [英] Java retrieve returned ArrayList to HashMap
问题描述
在我的简单的java应用程序我有这个函数返回的ArrayList
:
Items.java
类:
包com.example.Hadis;/ **
*创建者的礼服世界上14年8月1日。
* /
公共类项目{ 公众诠释身份证;
公共字符串的内容;
}
有关填充和返回结果的ArrayList功能:
DataBaseHelper.java
类的<code> getFavorite 功能:
公开的ArrayList&LT;项目&GT; getFavorite(){
ArrayList的&LT;项目&GT; ArrayList的=新的ArrayList&LT;项目&GT;(); 做{ 项目dbItems =新项目(); dbItems.id = cursor.getInt(0);
dbItems.content = cursor.getString(1);
arrayList.add(dbItems);
}而(cursor.moveToNext()); 返回数组列表;
}
我想取这个返回的ArrayList
和填充HashMap的,但我不能这部分项目程序
自定义地图的方法类:
静态地图&LT;整数,字符串&GT;地图=新的HashMap&LT;整数,字符串&GT;();
retrive 的ArrayList
,我的问题是这样的:
数据库处理器DB =新的数据库处理器(ApplicationHadith.this);
ArrayList的&LT;项目&GT;清单= db.getFavorite(); 迭代器&LT;项目&GT;它= list.iterator();
而(it.hasNext())
{ // map.put(it.id,it.content) }
如何以检索 getFavorite()
,并填写的HashMap
?请帮助me.Thanks
只需使用一个简单的for循环例如
的(项目编号:名单){
map.put(item.id,item.content);
}
我通常不会在这种情况下使用的Iterator
。该增强上述从Java 5. <日期 - 循环语法 / p>
In my simple java application i have this function to return ArrayList
:
Items.java
class:
package com.example.Hadis;
/**
* Created by tux-world on 8/1/14.
*/
public class Items {
public int id;
public String content;
}
ArrayList function for fill and return result:
DataBaseHelper.java
class's getFavorite
function :
public ArrayList<Items> getFavorite(){
ArrayList<Items> arrayList = new ArrayList<Items>();
do {
Items dbItems = new Items();
dbItems.id = cursor.getInt(0);
dbItems.content = cursor.getString(1);
arrayList.add(dbItems);
} while (cursor.moveToNext());
return arrayList;
}
i want to fetch this return ArrayList
and fill HashMap but i can not program this part of project
Defined map as Method in class:
static Map<Integer,String> map = new HashMap<Integer,String>();
retrive ArrayList
and my problem is this:
DatabaseHandler db = new DatabaseHandler(ApplicationHadith.this);
ArrayList<Items> list = db.getFavorite();
Iterator<Items> it = list.iterator();
while(it.hasNext())
{
// map.put( it.id , it.content )
}
how to retrive getFavorite()
and fill HashMap
? please help me.Thanks
Just use a simple for loop e.g.
for (Item item : list) {
map.put( item.id, item.content);
}
I wouldn't normally use an Iterator
in this scenario. The enhanced for-loop syntax above dates from Java 5.
这篇关于Java的找回返回的ArrayList到HashMap类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!