java - Assign ArrayList element if int variable is equal to position in ArrayList -


i want compare int named id index of myarraylist. if there match (id = 1 , there element @ index), want assign string myname value of item @ position. this, used:

for (int =0; <myarraylist.size (); i++) {     if (id == i) {        myname = myarraylist.get(i);     }  

but error "cannot covert myobject string" @ line:

myname = myarraylist.get(i); 

here whole code:

int id = 1; string myname;   public class myobject  {   public string name;    public  myobject( string name)    {     this.name = name;   }     @override     public string tostring()   {     return (name);   } }   arraylist<myobject> myarraylist = new arraylist<myobject>();  void setup()  {   size(100, 60);   myarraylist.add(new myobject("a"));   myarraylist.add(new myobject("b"));   myarraylist.add(new myobject("c"));   (int =0; <myarraylist.size (); i++) {     if (id == i) {        myname = myarraylist.get(i);     }       system.out.println(myarraylist);     myarraylist.removeall(myarraylist);   } } 

i don't understand why error since myname string , arraylist contains strings. me solve ?

thanks help

actually, arraylist contains "myobject"s. try calling tostring method.

myname = myarraylist.get(i).tostring();

or

myname = myarraylist.get(i).name since name public.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -