这段代码中的arraylist会发生什么?

时间:2010-10-28 11:57:10

标签: java arraylist

ArrayList userItem = new ArrayList();

userItem.add(item.getUserId()+"|"+item.getEmail()+"|"+item.getImgInstance());

ArrayList userItem = onlineUsers.get(item.getImgInstance());

我想知道最后一行会对列表做什么,它会将onlineUsers.get(item.getImgInstance())的值附加到上一个字符串中还是其他东西?它如何跟踪项目的添加?

P.s如果可以请也解释一下ArrayList的结构。

谢谢 编辑:

对不起,你们误解了我想要问的问题,因为我没有把完整的代码放到实际上这个

的HashMap> onlineUsers = new HashMap(100);

              for(DBPresence item : listPresence){

                    if(onlineUsers.containsKey(item.getImgInstance())){
                            ArrayList userItem = onlineUsers.get(item.getImgInstance());
                            userItem.add(item.getUserId()+"|"+item.getEmail()+"|"+item.getImgInstance());
                    }else{
                            ArrayList userItem = new ArrayList();
                            userItem.add(item.getUserId()+"|"+item.getEmail()+"|"+item.getImgInstance());
                            onlineUsers.put(new Integer(item.getImgInstance()),userItem);
                    }
                }
                return new DBPresenceResponse(onlineUsers, _encapusulationText);

4 个答案:

答案 0 :(得分:1)

  

ArrayList userItem = new ArrayList();

应该是

List userItem = new ArrayList();   

您在这里添加一个String对象

  

userItem.add(item.getUserId()+ “|” + item.getEmail()+ “|” + item.getImgInstance());

你正试图从这里的List中检索对象

  

onlineUsers.get(item.getImgInstance())

这里item.GetImgInstance()应该返回一个数据类型,可以将转换器隐式转换为int

检查Docs

答案 1 :(得分:1)

ArrayList有一个支持数组,用于保存数据。添加项目时,数组将复制到一个新的更大的数组中。

上面代码的作用超出了我的范围 - 它甚至都没有编译,因为你要定义名为userItem的列表两次。

更新:上述代码的要点是检查给定密钥(图像实例)是否存在列表,如果不存在,则创建一个新列表并将其放入地图中。如果存在 - 获取它,并向其添加新记录。

答案 2 :(得分:1)

// A new ArrayList is created. An ArrayList is a dynamic array that can hold
// any type of object. If you just need String object, use ArrayList<String>.
ArrayList userItem = new ArrayList();

// A String is added to the ArrayList.
userItem.add(item.getUserId()+"|"+item.getEmail()+"|"+item.getImgInstance());

// *Error*, you are defining a new ArrayList reference with the same name 
// than the previous one.
ArrayList userItem = onlineUsers.get(item.getImgInstance());

要修复错误,您有4个选择:

选择1

// if onlineUsers.get() returns an ArrayList, this choice will throw the
// previous ArrayList to the trash can (also named the garbadge collector)
// and you won't be able to retrieve its information.
userItem = onlineUsers.get(item.getImgInstance());

选择2

// if onlineUsers.get() returns an ArrayList, this choice will append
// its elements to the previous arraylist.
userItem.addRange(onlineUsers.get(item.getImgInstance()));

选择3

// if onlineUsers.get() *does not* return an array, this choice let you 
// append it to the arraylist.
userItem.add(onlineUsers.get(item.getImgInstance()));

选择4

// Here, you are creating a NEW arraylist with a different reference name.
// It has no links at all with the previous one.
ArrayList userItem2 = onlineUsers.get(item.getImgInstance());

实际上还有很多其他选择,但主要是这些选择。

答案 3 :(得分:0)

这里当您将userItem分配给新的List onlineUsers.get(item.getImgInstance());

这不会附加onlineUsers.get(item.getImgInstance());  将项目列出到您的userItem列表