English 中文(简体)
1. 物体与代号物体比较
原标题:Compare object with deserialised object

我有一位资深的法国人申请,他们与一个习俗清单模型合作。

该模型有一架ArrayList储存所使用的物体。 情况如下:

在申请结束时,所有物体均按序编号(以缺省方式,该类别仅执行<代码>可乘),并通过<代码”书写。 ObjectOutputStream。 当申请启动时,所有物体均从档案中读取,并再次储存在我的习惯上

我试图增加一个特点,让用户也从他具体规定的档案中进口物体。 另一类静态方法将所有物体从档案中读取,并将其填入。 接着,我使用我的<代码>MainForm的每 lo,在<代码>ListModel上储存每一物体。 在行文中,我要检查<代码>ListModel。 某些物体,,但并不有效

在法典中,我做以下工作:

for(MyObject o: readObjects) {
    if(!myListModel.contains(o)) //listmodel just calls contains() on its ArrayList
        myListModel.addElement(o);
}

然而,即使该物体已经位于ArrayList(我从同一档案中进口物体),该物体仍为 添加

问题在于,在帝国化时,不会再有任何相同之处?

最佳回答

这里是一个简单的java物体

public class MyObject {

private String firstname;
private String lastname;
private String email;

public String getFirstname() {
    return firstname;
}
public void setFirstname(String firstname) {
    this.firstname = firstname;
}
public String getLastname() {
    return lastname;
}
public void setLastname(String lastname) {
    this.lastname = lastname;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((firstname == null) ? 0 : firstname.hashCode());
    result = prime * result
            + ((lastname == null) ? 0 : lastname.hashCode());
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    MyObject other = (MyObject) obj;
    if (firstname == null) {
        if (other.firstname != null)
            return false;
    } else if (!firstname.equals(other.firstname))
        return false;
    if (lastname == null) {
        if (other.lastname != null)
            return false;
    } else if (!lastname.equals(other.lastname))
        return false;
    return true;
}
}

(hashcode and Equals auto produced by eclipse)

如果你看上“平等”的方法,你就会看到,在第一个名称和最后名称的领域中,对两个物体进行了比较,只有那些物体。 2. 结 论 这意味着,如果你在一份清单中添加这类物体,你将能够使用<代码>contains(Object o),如果某一物体含有相同的第一个名称和最后名称,你就会发现。

问题回答

You need to override the equals and hashcode method of your object (many examples are available on SO, for example here.

一旦你这样做,名单的<编码>contains方法将按你的期望行事。 你的问题似乎与序列化无关。 换言之,以下法典:

List<MyObject> list = new ArrayList<MyObject>();
list.add(new MyObject());
System.out.println(list.contains(new MyObject()));

如果你不凌驾于“平等”和“计划”之上,就会印制假象,而如果你这样做的话,则可能照旧(取决于你的平等方法是否认为“第2条”(<>新MyObject()是平等的)。 你的民主选举学会应该有办法为你们制定法典。

一旦您高兴,<代码>contains方法是按预期在您的物体上运行的,序列生成/播种应当按预期运作。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...