tags:

views:

83

answers:

4
ArrayList userItem = new ArrayList();

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

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

I want to know what the last line will do to the list will it append the value of onlineUsers.get(item.getImgInstance()) in the previous string or somethign else? how does it keep track of the item's beign added?

P.s if you can please also explain the structure of ArrayList.

Thank you edited:

Sorry guys you have misunderstood what i was trying to ask cause i didnt put the complete code its actually this

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);
+1  A: 

ArrayList userItem = new ArrayList();

Should be

List userItem = new ArrayList();   

You are adding a String object here

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

YOu are trying to retrieve the object from List here

onlineUsers.get(item.getImgInstance())

Here item.GetImgInstance() should return a datatype that can be converter into int implicitly

Check Docs

org.life.java
*"Here item.GetImgInstance() must return int "* - not necessarily. We don't know what the type of `onlineUsers` is.
Stephen C
You don't always want to qualify a reference with an interface...
Aurélien Ribon
item.GetImgInstance() does return an int. what i wanted to knwo was that how will the arraylist react to this statement
Mohsin Sheikh Khalid
@Mohsin Sheikh Khalid To which statement exactly ?
org.life.java
+1  A: 

ArrayList has a backing array, which holds the data. When you add items, the array is copied into a new, larger array.

What the above code is doing is beyond me - it doesn't even compile, because you are defining the list named userItem twice.

Update: The point of the above code is to check whether a list exists for the given key (image instance), and if it does not, create a new one and put it in the map. If it exists - get it, and add a new record to it.

Bozho
thank you i knowing that it copies it into a new array when we add item is useful but what happens when we dont use the add statement and do something like this ArrayList userItem = onlineUsers.get(item.getImgInstance()); and for the declaring useritme twice well i am bound to do that cause of the if condition.
Mohsin Sheikh Khalid
A: 

here As you are allocating userItem to new List onlineUsers.get(item.getImgInstance());

this will not append onlineUsers.get(item.getImgInstance()); list items to your userItem List

sagar
+1  A: 
// 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());

To fix the error, you have 4 choices :

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

Choice 2

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

Choice 3

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

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

Actually there are many other choices but here are the main ones.

Aurélien Ribon