gpt4 book ai didi

JAVA长比较貌似不准确

转载 作者:行者123 更新时间:2023-12-01 11:21:22 24 4
gpt4 key购买 nike

遇到一个问题,我需要与长值进行比较并检查是否相等。我的相关代码如下:

for(int i = 0; i <=results.size()-1; i++) {
if(results.get(i).get(0).getTime() == results.get(i).get(results.get(i).size()-1).getTime()){
continue;
}
}

设置断点时,

results.get(i).get(0).getTime()     has value: 1329286731000
results.get(i).get(results.get(i).size()-1).getTime() has value: 1329286731000

但是比较返回 false。这里有什么想法吗?

注意:结果是一个列表>并且 LocationEvent 类可以在下面找到:

public class LocationEvent implements Serializable {

private static final long serialVersionUID = 1L;

/**
* The unique identifier for this event
*/
private Integer id;

/**
* The date and time that this location event was recorded in milliseconds since January 1, 1970, 00:00:00 GMT
*/
private Long time;

/**
* The latitude at which the event occurred
*/
private Double latitude;

/**
* * The longitude at which the event occurred
*/
private Double longitude;

/**
* The speed the resource was traveling at the time of the event in MPH
*/
private Double speed;

/**
* The direction the resource was traveling at the time of the event (i.e. N, S, NW, SE, etc).
*/
private String direction;

/**
* The altitude at which the event occurred in feet above sea level
*/
private Double altitude;

/**
* The closest known address to the location.
*/
private String nearestAddress;

/**
* Any available comment describing this location event
*/
private String comment;

/**
* The source of this event (i.e. GeoProductSolutions, GpsInsight, Multispeak, WirelessMatrix, WiSys, NiscMobile,
* AppSuite-iOS, AppSuite-Android)
*/
private String source;

/**
* The device to which this event is associated
*/
private LocationDeviceSummary device;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Long getTime() {
return time;
}

public void setTime(Long time) {
this.time = time;
}

public Double getLatitude() {
return latitude;
}

public void setLatitude(Double latitude) {
this.latitude = latitude;
}

public Double getLongitude() {
return longitude;
}

public void setLongitude(Double longitude) {
this.longitude = longitude;
}

public Double getSpeed() {
return speed;
}

public void setSpeed(Double speed) {
this.speed = speed;
}

public String getDirection() {
return direction;
}

public void setDirection(String direction) {
this.direction = direction;
}

public Double getAltitude() {
return altitude;
}

public void setAltitude(Double altitude) {
this.altitude = altitude;
}

public String getNearestAddress() {
return nearestAddress;
}

public void setNearestAddress(String nearestAddress) {
this.nearestAddress = nearestAddress;
}

public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = comment;
}

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}

public LocationDeviceSummary getDevice() {
return device;
}

public void setDevice(LocationDeviceSummary device) {
this.device = device;
}
}

最佳答案

如果您的结果是 Long s 而不是 long s,==运算符可能返回 false不论值(value)观是否平等。

您可能想使用equals或调用longValue()==进行比较。

注意

如果整数值在 Byte.MIN_VALUE <= x <- Byte.MAX_VALUE 范围内,则会被缓存,因此 ==该范围内的比较将返回 true

关于JAVA长比较貌似不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31190055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com