Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.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 如何比较2个LinkedList,但以Object作为参数?_Java - Fatal编程技术网

Java 如何比较2个LinkedList,但以Object作为参数?

Java 如何比较2个LinkedList,但以Object作为参数?,java,Java,这是我的学校作业。我不知道如何解决它,因为方法equals只允许传递一个参数。请给我一些提示 问题是: 添加public boolean equalsObject other方法,该方法在两个对象的内容相同时返回true。请注意,如果两个对象具有相同数量的项目,并且一个对象中的每个项目都等于另一个对象中相应位置的项目15点,则这两个对象是相同的 上述方法必须添加到LList2.java中 public class LList2<T> implements ListInterface&

这是我的学校作业。我不知道如何解决它,因为方法equals只允许传递一个参数。请给我一些提示

问题是:

添加public boolean equalsObject other方法,该方法在两个对象的内容相同时返回true。请注意,如果两个对象具有相同数量的项目,并且一个对象中的每个项目都等于另一个对象中相应位置的项目15点,则这两个对象是相同的

上述方法必须添加到LList2.java中

public class LList2<T> implements ListInterface<T> {
private Node firstNode; // head reference to first node
private Node lastNode;  // tail reference to last node
private int numberOfEntries;

public LList2() {
    clear();
} // end default constructor

public final void clear() // NOTICE clear is not final in interface and that is OK
{
    firstNode = null;
    lastNode = null;
    numberOfEntries = 0;
} ......
社会主义阶级

public class AList<T> implements ListInterface<T> {
private T[] list;   // array of list entries
private int numberOfEntries;
private static final int DEFAULT_INITIAL_CAPACITY = 25;

public AList() {
    this(DEFAULT_INITIAL_CAPACITY); // call next constructor
} // end default constructor

public AList(int initialCapacity) {
    numberOfEntries = 0;
    // the cast is safe because the new array contains null entries
    @SuppressWarnings("unchecked")
    T[] tempList = (T[]) new Object[initialCapacity];
    list = tempList;
} ......
java

public class LList2<T> implements ListInterface<T> {
private Node firstNode; // head reference to first node
private Node lastNode;  // tail reference to last node
private int numberOfEntries;

public LList2() {
    clear();
} // end default constructor

public final void clear() // NOTICE clear is not final in interface and that is OK
{
    firstNode = null;
    lastNode = null;
    numberOfEntries = 0;
} ......

这是一个实例方法——它将作为参数传递的对象与调用它的对象进行比较。请注意,您不需要显式地调用它,但我将其保留在那里以使代码更清晰:

@Override
public boolean equals(Object other) {
    // Check that other is even an AList
    if (!(other instanceof AList)) {
        return false;
    }

    // If it is, cast it and compare the contents:
    AList otherAList = (AList) other;

    // Compare the lenghts of the arrays
    if (this.numberOfEntires != otherAList.numberOfEntries) {
        return false;
    }

    // Compare the contents of the arrays:
    for (int i = 0; i < this.numberOfEntries; ++i) {
        if (!this.list[i].equals(otherAList.list[i])) {
            return false;
        }
    }

    // Didn't find a reason why the two aren't equal, so they must be:
    return true;
}

您的尝试是什么?您可以重写equals方法或使用Comparator按照说明,首先创建一个方法,如果大小匹配则返回true,如果大小不匹配则返回false,然后扩展该方法以检查大小和第一个元素,然后开始使用loopsone更多提示,搜索“和castingI”的实例我投了反对票,因为这基本上是一个请做我的家庭作业的问题你不应该做其他人的家庭作业。。。这不是StackOverflow的意图。最好是向接口而不是特定的implementation@Wisthler那是决定的问题。长度和内容相同的列表和其他列表是否应该被视为相等?如果是这样,一定要使用接口。如果没有,您应该坚持使用混凝土类。在任何情况下,铸件都应与instaceof checking相匹配。@Mureinik确实,这取决于要求。这在本文中不是很清楚,但在字里行间,练习的目标可能是在接口级别进行