I have made an ArrayList in Java and put some integers and some doubles in it, then I added an array of integers and an array of doubles. Now I have changed a value of one of the doubles that is in the array, and I want to update it. However, I’m having trouble with coming up with the exact syntax for it, since it’s a location within an array within an ArrayList.
To be more specific, I have
ArrayList<Patch> patches;
patches = new ArrayList<Patch>(); //the ArrayList of patches
int[] VisPatch; //the array of integers that keeps track of the patchID of the visible patches
double[] formFactor; //the array of doubles that keeps track of the formFactor for the visible patches
//these are brought in from a file using a scanner
int patchID = fin.nextInt(); //the patch number for each patch in the file
double emissionPower = fin.nextDouble(); //the emission power of the individual patch
double reflectance = fin.nextDouble(); //the reflectance of the individual patch
int numVisPatch = fin.nextInt(); //the number of patches that are 'visible' to this particular patch
//initialize the arrays that hold the visible patch parameters
VisPatch = new int[numVisPatch];
formFactor = new double[numVisPatch];
for(int i=0; i<numVisPatch; i++){
VisPatch[i] = fin.nextInt(); //brought in from file
formFactor[i] = fin.nextDouble();
}//end for loop
//create a new patch object from the numbers read in
patches.add(new Patch(emissionPower, reflectance, numVisPatch, VisPatch, formFactor));
//get the first visible patch in the VisPatch array
int adjacentPatchID = patches.get(maxKeyIndex).VisPatch[k]; //maxKeyIndex has been declared, and yes, we're in a for loop that uses k
//do some math on the emissionPower
double increment = 2;
double newEmissionPower = patches.get(adjacentPatchID).emissionPower + increment;
//now update the emission power of the patch
ummm...
I’ve tried
patches.set(adjacentPatchID, newEmissionPower);
and
patches.set(get(adjacentPatchID).emissionPower, newEmissionPower);
and
patches.set(adjacentPatchID.emissionPower, newEmissionPower);
but my IDE (I’m using Eclipse) just put a bunch of red squiggly lines under everything and says I don’t know what I’m talking about (mainly because I don’t).
I’m assuming the thing you want to update is in the
Patchclass, not in the list of Patches.You want to get the patch by ID (assuming your “id” in this case is its position in the list; if not, you might consider using a
Map<Integer, Patch>instead) and then mutate it: