Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing a random element of an arraylist yields the same results
    text
    copied!<p>I have a class called Keypoints and i want to create an array list storing various keypoints objects. This is how i decalred and initialise the Keypoints list:</p> <pre><code> private static List&lt;Keypoint&gt; m_keyPoints = new ArrayList&lt;Keypoint&gt;(); </code></pre> <p>so in my codes , i have a for loop which adds keypoints objects into the list :</p> <pre><code> for(xi=0;xi&lt;width;xi++) { for(yi=0;yi&lt;height;yi++) { /*Calculation for scale,mag,orien*/ // Save this keypoint into the list Keypoint kp =new Keypoint(xi*scale/2, yi*scale/2, mag, orien, i*m_numIntervals+j-1); m_keyPoints.add(kp); } } </code></pre> <p>But now whenever i try to access a random element from the list , I'm always getting the same results . </p> <pre><code> for(int ikp = 0;ikp&lt;m_numKeypoints;ikp++) { Keypoint key =m_keyPoints.get(ikp); int scale = key.s(); float kpxi = key.xi(); float kpyi = key.yi(); } </code></pre> <p>I'm am getting the same value for scale,kpxi, and kpyi for every element in the list even though I'm very sure that every value is different. Instead it's returning the last object in the list . So my question is , am I doing it wrongly when it comes to adding the object into the list or is it a problem with my keypoint class ? or is it something else altogether ? </p> <p>Below is the class for Keypoint</p> <pre><code>import java.util.List; public class Keypoint { public float xi; public float yi; public List&lt;Double&gt; orien ; public List&lt;Double&gt; mag; public int scale; public Keypoint(){} public Keypoint(float x, float y, List&lt;Double&gt; magnitude, List&lt;Double&gt; orientation, int s) { xi = x; yi = y; mag = magnitude; orien = orientation; scale = s; } public static float xi() { return xi; } public static float yi() { return yi; } public static int s() { return scale; } public static List&lt;Double&gt; mag() { return mag; } public static List&lt;Double&gt; orien() { return mag; } </code></pre> <p>}</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