Update a single variable of a class in an ArrayList of class in java -


i have class components:

public class components {      int numberofnets;      string nameofcomp;     string nameofcomppart;     int numofpin;      public components(int i, string compname, string partname, int pin) {         this.numberofnets = i;         this.nameofcomp = compname;         this.nameofcomppart = partname;          this.numofpin = pin;     }  } 

inside class created arraylist of components class:

list<components> complist = new arraylist<components>(); 

later in code, adding elements in list in way:

complist.add(new components(0,compname,partname,0)); 

see, here numberofnets , numofpin variables in components class initiated 0 values. these values getting calculated/incremented in later part of code , hence need update new values of these 2 variables in each list element. arraylist doc idea of updating list element using index set operation. confused how set/update particular variable of class in arraylist of class. need update these 2 mentioned variables, not of 4 variables in components class. there way that?

you should add getter/setter component class outer class can update component's members

public class components {      private int numberofnets;      private string nameofcomp;     private string nameofcomppart;     private int numofpin;      public components(int i, string compname, string partname, int pin) {         setnumberofnets(i);         setnameofcomp(compname);         setnameofcomppart(partname);          setnumofpin(pin);     }      public void setnumberofnets(int numberofnets) {         this.numberofnets = numberofnets;     }      // other getter , setters } 

you can modify data using following code because get() return reference original object modifying object update in arraylist

complist.get(0).setnumberofnets(newnumberofnets); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -