java - 需要添加一个 add 方法,但每次都会收到不同的错误,我不知道其含义

标签 java arrays arraylist compiler-errors incompatibletypeerror

在这个家庭作业中,我们将在名为 SimpleArrayList 的类中完成上面所示的 SimpleList 接口(interface)的实现。我们为您提供了起始代码,包括一个采用初始对象引用数组的构造函数。我们还提供了 get、set 和 size 方法的代码(您昨天已完成这些方法)以及删除方法的代码,以便让您了解如何处理 add。请注意,此时我们正在测试上面显示的整个界面。你的工作是完成添加。与 set 一样,您可以忽略无效索引。我们将测试它们。

我的代码在逻辑上是有意义的,但无法编译


public class SimpleArrayList {
    /** Internal array for storing values. */
    private Object[] array;

    /**
     * Create a list from an array of Objects.
     *
     * Copies references from the passed array so that
     * modifications to this list will not affect the original array.
     * We'll need to make copies of the array later to support add and remove,
     * so this is the right thing to do now.
     *
     * @param originalArray original array of Objects used to create the list
     */
    SimpleArrayList(Object[] originalArray) {
        // Would normally need to defend against originalArray being null,
        // but we'll defer that until later.
        if (originalArray != null) {
            array = new Object[originalArray.length];
            for (int i = 0; i < originalArray.length; i++) {
                array[i] = originalArray[i];
            }
        }
    }

    public Object get(int index) {
        if (index < 0 || index >= array.length) {
            return null;
        }
        return array[index];
    }
    public void set(int index, Object element) {
        if (index < 0 || index >= array.length) {
            return;
        }
        array[index] = element;
    }


    public int size() {
        return array.length;
    }

    public Object remove(int removeIndex) {
        if (removeIndex < 0 || removeIndex >= array.length) {
            return null;
        }

        // remove returns the item being removed
        Object toReturn = array[removeIndex];

        // Create and populate our new smaller array. We use for loop syntax
        // maintaining two indices.
        Object[] newArray = new Object[array.length - 1];
        int originalIndex = 0;
        for (int newIndex = 0; newIndex < newArray.length; newIndex++) {
            // Skip the spot that we are removing
            if (newIndex == removeIndex) {
                originalIndex++;
            }
            newArray[newIndex] = array[originalIndex];
            originalIndex++;
        }
        array = newArray;
        return toReturn;
    }

      public void add(int index, Object element)
    {
        Object[] newArray = new Object[array.length + 1];
        int orIndex = 0;

        for(int i =0; i< index; i++)
        {
            newArray[i] = array[orIndex];
            orIndex++;
        }

        newArray[index] = element;

        for(int i = index+1; i< newArray.length; i++)
            {
                newArray[i] = array[orIndex];
                orIndex++;
            }

        array = newArray;
    } 



}

除了我编写的 add 方法之外,所有这些都已给出。我现在收到不同的错误,它说将 simplearraylist 实例化为我什至没有编写的列表的不兼容类型。

我的代码在逻辑上是有意义的,但无法编译。我只写了add方法,其他的都已经给出了。我基于删除方法

最佳答案

它必须实现接口(interface)SimpleList

关于java - 需要添加一个 add 方法,但每次都会收到不同的错误,我不知道其含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57069631/

相关文章:

javascript - 使用 JavaScript 将数组填充到特定索引处的二维数组中

Java - 使用文件名数组对每个文件运行方法

java - 如果 ArrayList 的 clone() 被破坏了,为什么他们不能修复它呢?

java - Java “Foo f = new Foo() ”中的对象初始化是否与在C中将malloc用作指针相同?

java - 使用 mFusedLocationClient 获取 Firebase 服务中的当前位置

java - 无法访问也是子类的子类中的 protected 成员

java - 如何使用 onClick 将用户发送到 ChatActivity

arrays - MongoDB - 如何访问数组中的对象

java - 转换为 Array 时 Set 项的排序顺序

Java:使用比较器和属性的第二个字对对象列表进行排序