Java cordinates x和y数组包含在列表中
我有一个清单,我填写如下Java cordinates x和y数组包含在列表中,java,arrays,list,Java,Arrays,List,我有一个清单,我填写如下 List<Float[]> list = new ArrayList<>(); list.add(new Float[]{x,y}); List List=new ArrayList(); 添加(新浮点[]{x,y}); 我想在这个列表上做一个测试,a包含x和y,需要一个精确的数字,就像这样 private boolean containlist(float x, float y) { return (x &l
List<Float[]> list = new ArrayList<>();
list.add(new Float[]{x,y});
List List=new ArrayList();
添加(新浮点[]{x,y});
我想在这个列表上做一个测试,a包含x和y,需要一个精确的数字,就像这样
private boolean containlist(float x, float y) {
return (x <730 && x > 710 && y <1114 && y >140);
}
private boolean containlist(浮点x,浮点y){
返回(x 710和y 140);
}
您的问题有点模糊,但您可以创建一个类“坐标”
其中包含属性(x,y),然后创建此类的ArrayList。
之后,可以使用ArrayList的方法“contains”
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");
}
}
公共类坐标
{
私有浮动x,y
坐标()
{
}
坐标(浮动x、浮动y)
{
这个.x=x;
这个。y=y;
}
}
公共静态void main(字符串[]args){
ArrayList=新建ArrayList();
坐标c1=新坐标(1.5,2.3)
增加(c1);
//要查看元素是否在列表中,可以使用以下方法
如果(列表包含(c1))
{
System.out.println(“它在列表中”);
}
}
您的问题有点模糊,但您可以创建一个类“坐标”
其中包含属性(x,y),然后创建此类的ArrayList。
之后,可以使用ArrayList的方法“contains”
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");
}
}
公共类坐标
{
私有浮动x,y
坐标()
{
}
坐标(浮动x、浮动y)
{
这个.x=x;
这个。y=y;
}
}
公共静态void main(字符串[]args){
ArrayList=新建ArrayList();
坐标c1=新坐标(1.5,2.3)
增加(c1);
//要查看元素是否在列表中,可以使用以下方法
如果(列表包含(c1))
{
System.out.println(“它在列表中”);
}
}
又短又甜:
list.removeIf(xy -> !containlist(xy[0], xy[1]));
虽然您选择Float[]
来存储坐标是可疑的,但这与问题并不相关。简短而甜蜜:
list.removeIf(xy -> !containlist(xy[0], xy[1]));
虽然您选择Float[]
来存储坐标是可疑的,但这与问题无关。如果您的列表
和方法容器列表(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
}
如果您的列表
和方法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
}
我有一个列表,我想测试我的列表是否包含以下坐标!对于你的回答,我不太明白,你可以给我举个例子吗?但是如何知道我的坐标xy是否在x710和&y140之间我有一个列表,我想测试我的列表是否包含以下坐标!对于你的回答,我不太明白你能给我举个例子吗?但是如何知道我的坐标xy是否在x710和&y140之间