繁体   English   中英

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

[英]What will happen to arraylist in this code?

ArrayList userItem = new ArrayList();

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

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

我想知道最后一行将对列表执行什么操作,它将在上一个字符串或其他字符串中附加onlineUsers.get(item.getImgInstance())的值吗? 如何跟踪添加的物品的外观?

如果可以的话也请说明ArrayList的结构。

谢谢编辑:

抱歉,您误会了我想问的问题,因为我没有完整输入代码,实际上这是

HashMap> onlineUsers =新的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);

ArrayList userItem = new ArrayList();

应该

List userItem = new ArrayList();   

您要在此处添加String对象

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

您正在尝试从此处的列表中检索对象

onlineUsers.get(item.getImgInstance())

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

检查文件

ArrayList具有支持数据的后备数组。 添加项目时,该数组将复制到一个更大的新数组中。

上面的代码正在做的事情超出了我的范围,它甚至没有编译,因为您两次定义了名为userItem的列表。

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

// 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());

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

当您将userItem分配到新列表上时onlineUsers.get(item.getImgInstance());

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM