英文:
Javafx List<Data<Number,Number>> removeAll didn't work
问题
我有这样一个代码段:
List<Data<Number, Number>> dataList = new ArrayList<XYChart.Data<Number,Number>>();
List<Data<Number, Number>> dataListSelected = new ArrayList<XYChart.Data<Number,Number>>();
我用一些Data<Number,Number>(实际上是一些Double)填充它们。我为这两个列表执行以下操作:
for (XYZPointModel xyz : pm.getTopoPoints()) {
topoPoints.getData().add(new Data<Number, Number>(xyz.getX(),xyz.getY()));
}
然后我比较这两个列表。我想知道dataList中是否有一些数据不在dataListSelected中。
System.out.println(dataList.removeAll(dataListSelected));
我得到了一个FALSE。
这里是我两个列表的内容:
*3:[Data[18.367963,-0.832832,null],Data[30.165189,-0.461874,null],Data[18.808959,-6.575699,null]]*
*3:[Data[18.367963,-0.832832,null],Data[30.165189,-0.461874,null],Data[18.808959,-6.575699,null]]*
顺便说一下,我不知道为什么会有一个double,double,null。我不知道这个null是从哪里来的。
为什么我的removeAll方法不起作用?
英文:
I have this :
List<Data<Number, Number>> dataList = new ArrayList<XYChart.Data<Number,Number>>();
List<Data<Number, Number>> dataListSelected = new ArrayList<XYChart.Data<Number,Number>>();
I fill them with some Data<Number,Number> (in reality it is some Double). I do this for the two list.
for (XYZPointModel xyz : pm.getTopoPoints()) {
topoPoints.getData().add(new Data<Number, Number>(xyz.getX(),xyz.getY()));
}
and then i compare the two list. I want to know if i have some data in dataList that i dont have in dataListSelected.
System.out.println(dataList.removeAll(dataListSelected));
I got a FALSE.
Here the containt of my 2 list :
3 : [Data[18.367963,-0.832832,null], Data[30.165189,-0.461874,null], Data[18.808959,-6.575699,null]]
3 : [Data[18.367963,-0.832832,null], Data[30.165189,-0.461874,null], Data[18.808959,-6.575699,null]]
Btw, i don't know why i have a double, double, null. I dont know where the null come from.
Why my removeAll don't work ?
答案1
得分: 1
我推测 Data 实际上是 JavaFX.XYChart.Data,根据文档,它具有另一个对象节点。因此,这是 null。我不知道 "toString" 方法是否会打印节点对象的值,但看起来是这样的。
英文:
I presume Data is actually JavaFX.XYChart.Data, which has another object node as per docs. Therefore, this is null. I do not know if the "toString" method prints the value of the node object, but it sure seems like it.
答案2
得分: 1
ArrayList(以及许多其他集合)使用contains()(它使用equals()方法)来确定是否在remove()/removeAll()方法中移除一个对象。如果您检查以下条件:
dataList.get(0).equals(dataListSelected.get(0))
我的猜测是它将返回false,因此removeAll也会失败。
英文:
ArrayList (and many other collections) use the contains() (which uses equals() method) to determine whether to remove an object in the remove()/removeAll() methods. if you check the following condition:
dataList.get(0).equals(dataListSelected.get(0))
my guess is it would be false, and therefore removeAll fails as well.
答案3
得分: 0
1、ArrayList使用equals()来移除元素,
if (o.equals(elementData[i]))
return i;
但XYChart.Data没有重写equals()方法,因此Data将使用Object的方法
public boolean equals(Object obj) {
return (this == obj);
}
2、XYChart.Data有三个属性
X xValue,Y yValue,Object extraValue
分别是double、double、null。
英文:
1、ArrayList use equals() to remove,
if (o.equals(elementData[i]))
return i;
but XYChart.Data didn't override equals() method, so Data will use method of Object
public boolean equals(Object obj) {
return (this == obj);
}
2、XYChart.Data has three properties
X xValue, Y yValue, Object extraValue
double, double, null
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论