SortedSet、数组、可序列化的序列化问题

2022-01-17 00:00:00 list set serialization java sortedset

我在处理之前有这个:

protected void onPostExecute(SortedSet<RatedMessage> result) {
    List<Object> list=Arrays.asList(result.toArray());
    lancon.putExtra("results", list.toArray()); // as serializable
}

然后在另一部分我有

Object o=this.getIntent().getSerializableExtra("results");
//at this point the o holds the correct value (checked by debugger)
RatedMessage[] rm = (RatedMessage[]) o;// this line hangs out w ClassCastException
resultSet = new TreeSet<RatedMessage>(new Comp());
Collections.addAll(resultSet, rm);

为什么我得到 ClassCastException?

Why I get the ClassCastException?

推荐答案

我终于让它这样工作了:

Finally I got it to work this way:

Serializable s = this.getIntent().getSerializableExtra("results");
Object[] o = (Object[]) s;
if (o != null) {
    resultSet = new TreeSet<RatedMessage>(new Comp());
    for (int i = 0; i < o.length; i++) {
        if (o[i] instanceof RatedMessage) {
            resultSet.add((RatedMessage) o[i]);
        }
    }
}

相关文章