Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update an element in an array that is in an arraylist in java
    text
    copied!<p>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.</p> <p>To be more specific, I have</p> <pre><code>ArrayList&lt;Patch&gt; patches; patches = new ArrayList&lt;Patch&gt;(); //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&lt;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... </code></pre> <p>I've tried </p> <pre><code>patches.set(adjacentPatchID, newEmissionPower); </code></pre> <p>and</p> <pre><code>patches.set(get(adjacentPatchID).emissionPower, newEmissionPower); </code></pre> <p>and</p> <pre><code>patches.set(adjacentPatchID.emissionPower, newEmissionPower); </code></pre> <p>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).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload