[英]Add a hashmap to an array of hashmaps
I have the following code: 我有以下代码:
ResultSet rs = DB.doQuery("SELECT username,register_ip FROM users ORDER BY joined DESC LIMIT 10");
Map<String,String> botInfo = new HashMap<String, String>();
List<Map<String , String>> myMap = new ArrayList<Map<String,String>>();
int c = 0;
while(rs.next()) {
botInfo.put("username", rs.getString("username"));
botInfo.put("register_ip", rs.getString("register_ip"));
myMap.add(c, botInfo);
c++;
}
The idea is to have the output be like this.. and I'll use PHP as an example: 这个想法是让输出像这样..我将以PHP为例:
[0] => array('username' => 'someUsername', 'register_ip' => 'someIP');
And so on.. but, what I get is a list of 10 HashMaps that all contain the same username,register_ip value. 依此类推。.但是,我得到的是10个HashMap的列表,它们都包含相同的用户名,register_ip值。 What are my options? 我有什么选择?
That is because you're adding the same botInfo HashMap to your List. 那是因为您要将相同的botInfo HashMap添加到列表中。 What you would want to do is create a new HashMap inside your while loop, add elements to it and finally add this map to your list. 您想要做的是在while循环内创建一个新的HashMap,向其中添加元素,最后将此地图添加到列表中。 What you would want to do is something like this: 您想要做的是这样的:
ResultSet rs = DB.doQuery("SELECT username,register_ip FROM users ORDER BY joined DESC LIMIT 10");
List<Map<String , String>> myMap = new ArrayList<Map<String,String>>();
int c = 0;
while(rs.next()) {
//Create a new instance of the map
Map<String,String> botInfo = new HashMap<String, String>();
//Add elements to the the map
botInfo.put("username", rs.getString("username"));
botInfo.put("register_ip", rs.getString("register_ip"));
//Add the map to the list
myMap.add(c, botInfo);
//Increment your counter
c++;
}
ResultSet rs = DB.doQuery("SELECT username,register_ip FROM users ORDER BY joined DESC LIMIT 10");
List<Map<String , String>> myMap = new ArrayList<Map<String,String>>();
while(rs.next()) {
Map<String,String> botInfo = new HashMap<String, String>();
botInfo.put("username", rs.getString("username"));
botInfo.put("register_ip", rs.getString("register_ip"));
myMap.add(botInfo);
}
You need a new HashMap
for botInfo
for every record. 您需要为每个记录的botInfo
使用新的HashMap
。
EDIT: c
was useless 编辑: c
是没有用的
ResultSet rs = DB.doQuery("SELECT username,register_ip FROM users ORDER BY joined DESC LIMIT 10");
Map<String,String> botInfo;
List<Map<String , String>> myMap = new ArrayList<Map<String,String>>();
while(rs.next()) {
botInfo = new HashMap<String, String>();
botInfo.put("username", rs.getString("username"));
botInfo.put("register_ip", rs.getString("register_ip"));
myMap.add(botInfo);
}
Use this code.. This will help you. 使用此代码。这将为您提供帮助。
Try this one: 试试这个:
ResultSet rs = DB.doQuery("SELECT username,register_ip FROM users ORDER BY joined DESC LIMIT 10");
Map<String,String> botInfo;
List<Map<String , String>> myMap = new ArrayList<Map<String,String>>();
String[] columns = {"username", "register_ip"};
while(rs.next()) {
botInfo = new HashMap<String, String>();
for(int i = 0; i < columns.length; i++) {
botInfo.put(columns[i], rs.getString(columns[i]));
}
myMap.add(botInfo);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.