Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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中不使用ArrayList为类创建动态数组,并且没有被正确地教授如何这样做_Java - Fatal编程技术网

在Java中不使用ArrayList为类创建动态数组,并且没有被正确地教授如何这样做

在Java中不使用ArrayList为类创建动态数组,并且没有被正确地教授如何这样做,java,Java,所以我和一位老师一起上Java课,他不擅长教我们做家庭作业需要什么 在这个赋值过程中,我们应该创建一个包含构造函数、方法和必需特性的动态整数数组类。是不允许使用ArrayList的。我已经写了一些ArrayList,一些ArrayList不工作,其他的我不知道怎么做 以下是要求: 私有int数组[]字段。必须将数据内部存储在 整数的正则部分填充数组。请不要使用 ArrayList。分配的阵列的大小是其容量,并将 将在下面讨论 私有整数大小字段。此变量存储 数组中的“已占用”元素。在构造函数中设置

所以我和一位老师一起上Java课,他不擅长教我们做家庭作业需要什么

在这个赋值过程中,我们应该创建一个包含构造函数、方法和必需特性的动态整数数组类。是不允许使用ArrayList的。我已经写了一些ArrayList,一些ArrayList不工作,其他的我不知道怎么做

以下是要求:

  • 私有int数组[]字段。必须将数据内部存储在 整数的正则部分填充数组。请不要使用 ArrayList。分配的阵列的大小是其容量,并将 将在下面讨论
  • 私有整数大小字段。此变量存储 数组中的“已占用”元素。在构造函数中设置为0

  • 带参数的构造函数。该参数定义了设备的容量 初始数组。分配给定容量的数组,将大小字段设置为 零如果给构造函数的参数小于0, 正在抛出IllegalArgumentException

  • 没有参数构造函数。分配大小为10的数组,设置大小字段 到0

  • 复制构造函数。构造函数接受类型为的对象 DynamicArray作为参数,并将其复制到对象中 创造。如果 传递给复制源的对象为null

  • int getSize()返回大小–数组中占用的元素数

  • int[]toArray()访问器返回数组。确保你没有 返回私有数组字段。相反,为新的内存分配内存 数组,将数组字段复制到该新对象中,并返回 新阵列

  • public void push(int num)将新元素添加到数组的末尾 并递增“大小”字段。如果阵列已满,则需要 增加阵列的容量: A.创建一个新阵列,其大小等于原始阵列容量的两倍。 B将所有元素从数组字段复制到新数组。 C将新元素添加到新数组的末尾。D使用新数组作为数组字段

  • public int pop()抛出RuntimeException,删除 数组并返回它。减小大小字段。如果数组是 empty必须创建带有消息“Array is empty”的RuntimeException 扔。此时,请检查阵列的容量。如果 容量是占用单元数的4倍 (大小),现在是缩小阵列的时候了:

    a。创建一个新阵列,其大小等于 原版的。 B将所有元素从数组字段复制到新数组。 C使用新数组作为数组字段

  • int get(int index)抛出IndexOutfBoundsException返回元素 具有请求索引的数组的。如果提供的索引太多 无论是大的还是负数,都会使用 消息“非法索引”

  • int indexOf(int key)返回第一次出现的 给定的数字。当找不到数字时返回-1

  • void add(int index,int num)抛出IndexOutOfBoundsException adds 一个新元素(作为参数num传递)到 由索引参数指定的数组。如果索引大于 数组的大小或小于0,则IndexOutOfBoundsException为 扔。将元素添加到数组中间时, 您必须将所有元素向右移动,以便为 新的。如果阵列已满,且没有新阵列的空间 元素,数组的大小必须加倍。请按照步骤操作 在push()方法说明中列出,以使 阵列

  • int remove(int index)抛出IndexOutOfBoundsException删除 此数组中指定位置的元素。当 元素从数组的中间移除,所有元素 必须移动以闭合由移除的图元创建的间隙。如果 传递到方法的索引值大于或等于大小或 小于0时,必须抛出IndexOutOfBoundsException。在这 点检阵列的容量。如果容量是4倍 大于已占用元素的数量(大小),是时候 缩小阵列

  • 下面是我到目前为止所做的方法的类

    /**
     *
     * @author Lisa Hergert
     */
    public class DynamicArray {
        private int array[];
        private int size;
    
        /*
        * Constructor
        * @param capacity - integer
        * throws an IllegalArgumentExeception if capacity is less than 0
        */
        public DynamicArray (int capacity) {
            if (capacity < 0) {
                throw new IllegalArgumentException("Size cannot be less than 0.");
            }
        }
    
        /*
        * no-arg constructor
        */
        public DynamicArray () {
            array = new int [10];
            size = 0;
        }
    
        /*
        * Copies the array to a new one
        * @param - array [] - integer array
        */
        public DynamicArray (int array[]) {
            int arrayCopy [] = new int [array.length];
            for (int i = 0; i < array.length; i++) {
                arrayCopy[i] = array[i];
            }
        }
    
        /*
        * getSize returns the size.
        * @return - size
        */
        public int getSize () {
            return size;
        }
    
        /*
        * @param array[] - integer
        * @return array
        */
        public int [] toArray (int array[]) {
            return array;
        }
    
        /*
        * @param num - integer
        */
        public void push (int num) {
    
        }
    
        /*
        * @return pop - integer
        */
        public int pop() throws RuntimeException { 
            return pop();
        }
    
        /*
        * @param index - integer
        */
        public int get(int index) throws IndexOutOfBoundsException {
            if (index >= size || index < 0)
                throw new IndexOutOfBoundsException("Illegal Index");
            return array[index];
        }
    
        public int indexOf(int key) {
            int index = 0;
    
            return index;
        }
    
        public void add(int index, int num) throws IndexOutOfBoundsException {
            if (index >= size || index < 0)
                throw new IndexOutOfBoundsException("Illegal Index");
            int oldValue = array[index];
            array[index] = num;
            return oldValue;
        }
    
        public int remove(int index) throws IndexOutOfBoundsException {
    
        }
    }
    
    我知道这是不对的。即使你不想给我看代码,你能不能给我推荐一个网站,我可以查一下我需要做什么,这个网站的要求是#9

    谢谢你的帮助。我希望我有知识在没有帮助的情况下做到这一点,但事实并非如此,因此我不得不使用外部资源。这个网站在过去非常有用

    public int pop()抛出RuntimeException,删除 数组并返回它。减小大小字段。如果数组是 empty必须创建带有消息“Array is empty”的RuntimeException 扔。此时,请检查阵列的容量。如果容量 是占用元素数(大小)的4倍,则为 缩小阵列的时间:

    a。创建一个新阵列,其大小等于 原来的那个。B将数组字段中的所有元素复制到 新阵列。C使用新数组作为数组字段

    目前,您的pop命令正在递归地自行运行。这会崩溃

    编辑:我建议在pop之前部分执行
    删除
    ,因为这将使pop更容易。

    让我们把它分解成一个要点列表,并检查每个项目。(我还建议针对您需要的每一条建议提出一个新问题)

  • public int pop()抛出运行时异常
  • 删除数组的最后一个元素并返回它
  • 减量
    /*
    * @return pop - integer
    */
    public int pop() throws RuntimeException { 
        return pop();
    }
    
    public final class DynamicArray {
    }
    
    private int[] array;
    
    private int size;
    
    public DynamicArray(int capacity) {
        if (capacity < 0)
            throw new IllegalArgumentException("Capacity cannot be less than 0.");
        array = new int[capacity];
    }
    
    public DynamicArray() {
        this(10);
    }
    
    private static final int DEFAULT_CAPACITY = 10;
    
    public DynamicArray() {
        this(DEFAULT_CAPACITY);
    }
    
    public DynamicArray(DynamicArray source) {
        if (source == null)
            throw new IllegalArgumentException("Source array must not be null.");
        this.array = Arrays.copyOf(source.array, source.array.length);
        this.size = source.size;
    }
    
    public int getSize() {
        return size;
    }
    
    public int[] toArray() {
        return Arrays.copyOf(array, array.length);
    }
    
    public void push(int num) {
        growIfNecessary();
        array[size] = num;
        size += 1;
    }
    
    public void push(int num) {
        growIfNecessary();
        array[size++] = num;
    }
    
    public int pop() {
        if (size == 0)
            throw new NoSuchElementException();
        int result = array[--size];
        shrinkIfNecessary();
        return result;
    }
    
    public int get(int index) throws IndexOutOfBoundsException {
        if (index >= size || index < 0)
            throw new IndexOutOfBoundsException("Illegal index");
        return array[index];
    }
    
    public int indexOf(int key) {
        for (int i = 0; i < size; i++)
            if (array[i] == key)
                return i; // found
        return -1; // not found
    }
    
    public void add(int index, int num) throws IndexOutOfBoundsException {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException("Illegal index");
        growIfNecessary();
        System.arraycopy(array, index, array, index + 1, size - index);
        array[index] = num;
        size += 1;
    }
    
    public int remove(int index) {
        if (index >= size || index < 0)
            throw new IndexOutOfBoundsException("Illegal Index");
        int result = array[index];
        System.arraycopy(array, index + 1, array, index, size - index - 1);
        size -= 1;
        shrinkIfNecessary();
        return result;
    }
    
    private void growIfNecessary() {
        if (size == array.length) {
            if (array.length == 0)
                array = new int[1]; // Special case for capacity 0.
            else
                array = Arrays.copyOfRange(array, 0, array.length * 2);
        }
    }
    
    private void shrinkIfNecessary() {
        if (size * 4 <= array.length)
            array = Arrays.copyOfRange(array, 0, array.length / 2);
    }