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