如何比较由属于同一超类但属于不同子类的对象实例组成的两个数组列表。

How to compare two array lists that consist of instances of objects that belong to the same superclass, but various subclasses.

到目前为止我想到了。

public static boolean challenge(ArrayList<Thing> required, ArrayList<Thing> owned){

    boolean result=false;

    for(int i=0; i<=required.size(); i++)
    {
        for(int j=0; j<=owned.size(); j++)
        {
            ///compare each required to all owned
            ///if (required.get(i) instanceof owned.get(j))
            {   result=true;
            }

            /// else
            {
                result=false;
                break;
            }
        }   
    }

    return result;
}

它在 if 语句中给我编译错误。 “')' 预期“

我有点迷茫。任何帮助将不胜感激。

您可能想改用 required.get(i).getClass() == owned.get(j).getClass()

也可以考虑使用 'for each' 例如

for(Thing r : required) {
    for (Thing o : owned) {
      if (r.getClass() ...
        ...
    }
}

正如 Alex Nevidomsky 所说,instanceof 应该与右侧的类型一起使用。应该是if (required.get(i) instanceof owned.get(j).getClass())maybe this link will be helpful also