2017-02-26 104 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[]存儲座標是可疑的,這並不是真的有關題。