Saturday, 15 February 2014

java - Adding multiple objects to an ArrayList results in only the last item being added multiple times -



java - Adding multiple objects to an ArrayList results in only the last item being added multiple times -

i'm trying add together objects arraylist, seems whenever phone call add together method, list populated lastly object add.

here's how add:

myobject testobject1 = new myobject(); testobject1.settype(0); mylist.mylist.add(testobject1); myobject testobject2 = new myobject(); testobject2.settype(1); mylist.mylist.add(testobject2);

mylist class single arraylist defined follows:

public static arraylist<myobject> mylist = new arraylist<myobject>();

myobject class fellow member variables , methods:

static int type; public void settype(int intype) { type = intype; }

i list out objects in mylist.mylist follows:

for (int i=0; i<mylist.mylist.size(); i++) { log.d(tag, "type " + mylist.mylist.get(i).gettype()); }

it lists 2 objects, type 1.

what's up?

thanks.

because settype static, isn't associated object's state

myobject.settype = 1;

remove static keyword settype 's delcaration in myobject class , set value per instance

see

class fellow member variable

java android arraylist

No comments:

Post a Comment