1

I have a list that I fill out as follows

  List<Float[]> list = new ArrayList<>();
    list.add(new Float[]{x,y});

I want to do a test on this list a contains the x and y need to have a precise number just like this

 private boolean containlist(float x, float y) {

        return (x <730 && x > 710 && y <1114  && y >140);

}
  • Possible duplicate of [What is the best way to filter a Java Collection?](http://stackoverflow.com/questions/122105/what-is-the-best-way-to-filter-a-java-collection) – gus27 Feb 26 '17 at 15:08

3 Answers3

0

Your question is a bit vague , but you can create a class "coordinates" which has the attributes ( x,y ) then create an ArrayList of this class. after that you can use the method "contains" of the ArrayList

public class Coordinates
{
    private float x,y

    Coordinates()
    {

    }
    Coordinates(float x, float y)
    {
        this.x=x;
        this.y=y;
    }
}


public static void main(String[] args) {

ArrayList<Coordinates> list = new ArrayList<>();
Coordinates c1= new Coordinates(1.5, 2.3)
list.add(c1);

// to look if an element is inside the list you can use the method
if (list.contains(c1))
{
    System.out.println("It's inside the list");
}
}
0

Short and sweet:

    list.removeIf(xy -> !containlist(xy[0], xy[1]));

While your choice of Float[] to store coordinates is dubious, that is not really relevant to the question.

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
0

If your List<Float[]> list and method containList(float, float) are located in the same class, you can find the target coordinate (x, y) by iterating the list:

private boolean containList(float x, float y) {
    for (Float[] coordinate : list) {
        float coordinateX = coordinate[0];
        float coordinateY = coordinate[1];

        if (coordinateX == x && coordinateY == y) {
            return true;
        }
    }
    return false;  // (x, y) not found
}
Mincong Huang
  • 5,284
  • 8
  • 39
  • 62