deep_geek
deep_geek

Reputation: 347

Change a value of list java

I have an object (sonde(x,y,r,position)) whene i create a list of sonde i want to change value of position i use :

    int sizel =arSonde.size();
    for(int i=0;i<sizel;i++){
       double x1=arSonde.get(i).x;
       double y1=arSonde.get(i).y;
       double rayn=arSonde.get(i).rayon;
       double x2=arrayAction.get(0).x;
       double y2=arrayAction.get(0).y;
       Random random=new Random();

       double D =rand.nextDouble();
       double rayA=arrayAct.get(0).rayon;
       if(D<rayA){
        arSonde.set(pos,5);
       }

i used:

list.set(pos,element);

Upvotes: 0

Views: 77

Answers (1)

Adam
Adam

Reputation: 36703

Assuming you're trying set the position attribute of your Sonde object, and given your fields appear to be public...

arSonde.get(i).position = 5;

Your call arSonde.set(pos, 5) doesn't work because

  • pos isn't defined, or at least not defined in the code you've shared
  • set() expects (index, element) - index is the integer index within the array, not the name of field

If Sonde is immutable, or you needed a different instance you could always create a new instance and use the set(index, object) call...

arSonde.set(i, new Sonde(x1, y1, rayn, 5));

Upvotes: 1

Related Questions