Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA长比较似乎不准确_Java_Equality - Fatal编程技术网

JAVA长比较似乎不准确

JAVA长比较似乎不准确,java,equality,Java,Equality,有一个问题,我需要比较长值并检查是否相等。我的相关代码如下所示: 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; } } 然而,这个比较结果是错误的。有什么想法吗 注意:结果是一个列表>,

有一个问题,我需要比较长值并检查是否相等。我的相关代码如下所示:

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;
        }
}
然而,这个比较结果是错误的。有什么想法吗

注意:结果是一个列表>,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

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

注意


如果整数值在范围
Byte.MIN\u VALUE内,则会缓存整数值。请检查以下两个示例:

public class Test {
    public static void main(String[] args) {
        Long x=1329286731000l;
        Long y=1329286731000l;
        System.out.println(y==x);
    }
}
它返回false,因为它们相等,但它们是不同的对象:

public class Test {
    public static void main(String[] args) {
        long x=1329286731000l;
        long y=1329286731000l;
        System.out.println(y==x);
    }
}
它返回true,因为它比较值

您的
get(0).getTime()返回一个Long,因此您必须以fowlowing方式使用equals:

public class Test {
    public static void main(String[] args) {
        Long x=1329286731000l;
        Long y=1329286731000l;
        System.out.println(y.equals(x));
    }
}

变量的类型是什么?什么是
结果
?什么是
getTime
?在我们提供帮助之前,我们需要更多关于您的代码中到底发生了什么的信息。这有关系吗?我检查的两个相等值都是long?results是一个自定义对象列表,getTime()返回一个以毫秒为单位的长时间。提供完整的代码。results是什么,它包含什么,数据类型等等非常感谢。。。一定有时间没想过使用.equals()不客气。如果它们是原语,则可以可靠地使用
=
public class Test {
    public static void main(String[] args) {
        Long x=1329286731000l;
        Long y=1329286731000l;
        System.out.println(y.equals(x));
    }
}