Javafx的List<Data<Number,Number>>的removeAll方法没有起作用。

huangapple go评论72阅读模式
英文:

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&lt;Data&lt;Number, Number&gt;&gt; dataList = new ArrayList&lt;XYChart.Data&lt;Number,Number&gt;&gt;();
List&lt;Data&lt;Number, Number&gt;&gt; dataListSelected = new ArrayList&lt;XYChart.Data&lt;Number,Number&gt;&gt;();

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&lt;Number, Number&gt;(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 xValueY yValueObject 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

huangapple
  • 本文由 发表于 2020年9月22日 16:13:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64005610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定