2017-02-26 51 views
1

我有我填写如下cordinates x和y数组包含列表

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

我想要做一个测试在这个列表中包含x和y需要有一个准确的数字只是一个列表这样

private boolean containlist(float x, float y) { 

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

} 
+0

可能的复制[什么是过滤Java集合的最好方法是什么? (http://stackoverflow.com/questions/122105/what-is-the-best-way-to-filter-a-java-collection) – gus27

回答

0

如果您List<Float[]> list和方法containList(float, float)位于同一个班级,你可以找到目标是通过遍历列表协调(x, y):中

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 
} 
0

你的问题是有点模糊,但你可以创建一个类“坐标” 具有的属性(X,Y),然后创建该类的一个ArrayList。 后,您可以使用该方法“包含”的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

我有一个名单,我想测试,如果我的列表包含以下坐标!为了你的回答,我不太明白你能给我一个例子吗? –

+0

我添加了一些代码来说明更 –

+0

但如何知道我的坐标x和y分别是x <730 && x > 710 &&ÿ<1114 && y > 140之间 –

0

简短而亲切:

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

在您选择的Float[]存储坐标是可疑的,这并不是真的有关题。