I have an ArrayList<float[]> inside which I am placing arrays of floats storing the cartesian values of a line ie (x0,y0, x1,y1).
every time I perfrom a .contains(), with a float array it returns false, even though I can see in the debugger that it exists. SO seems to be comparing the memory reference and not the actual values. Any way to make them compare the values?
public static void main (String[] args) {
ArrayList <float[]>drawnLines = new ArrayList<float[]>();
float[] line = new float[4];
line[0] = (float)5;
line[1] = (float)12;
line[2] = (float)55;
line[3] = (float)66;
drawnLines.add(line);
float[] linea = new float[4];
linea[0] = (float)5;
linea[1] = (float)12;
linea[2] = (float)55;
linea[3] = (float)66;
if (drawnLines.contains(linea)) {
System.out.println("contians");
}
else {
System.out.println(" does not contian");
}
}
This is because
line.equals(linea)is false.You need to wrap the
float[]with a class which defines what you mean by equal.However, it appear that using a class like Line would be a better choice.
prints