1

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");
    }
}

3 Answers 3

4

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.


public static void main(String[] args) {
    List<Line> drawnLines = new ArrayList<Line>();
    drawnLines.add(new Line(5, 12, 55, 66));
    Line linea = new Line(5, 12, 55, 66);
    if (drawnLines.contains(linea))
        System.out.println("contains " + linea);
    else
        System.out.println(" does not contain " + linea);
}

static class Line {
    final float x0, y0, x1, y1;

    Line(float x0, float y0, float x1, float y1) {
        this.x0 = x0;
        this.y0 = y0;
        this.x1 = x1;
        this.y1 = y1;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Line line = (Line) o;
        if (Float.compare(line.x0, x0) != 0) return false;
        if (Float.compare(line.x1, x1) != 0) return false;
        if (Float.compare(line.y0, y0) != 0) return false;
        return Float.compare(line.y1, y1) == 0;
    }

    @Override
    public String toString() {
        return "Line{" + "x0=" + x0 + ", y0=" + y0 + ", x1=" + x1 + ", y1=" + y1 + '}';
    }
}

prints

contains Line{x0=5.0, y0=12.0, x1=55.0, y1=66.0}
1
  • okay, and how do I go about wrapping float in a class to give it my definition of equal? Oct 20, 2011 at 16:13
0

You could write your own function for the task:

boolean listContains(List<float[]> list, float[] arr) {
  for (float[] elem : list) {
    if (Arrays.equals(elem, arr)) {
      return true;
    }
  }
  return false;
}

To use it in your code, change

if (drawnLines.contains(linea)) {

to

if (listContains(drawnLines, linea)) {
0

You need implement a method that check if the content of both vectors are the same:

private static boolean isTheSameVector(float [] vect1, float [] vect2){
   if (vect1.length != vect2.length) {
        return false;
   } else {
        for (int i=0; i< vect1.length; i++) {
            if (vect1[i] != vect2[i]) {
                return false;
            }
        }
        return true;
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.