根据测试文件实施方法时出现的问题

我能够使构造函数和容量方法起作用,但不知道为什么 size()、isFull() 和 isEmpty() 失败。我相信它非常简单,但我只是无法看到一个小错误并修复它。希望有人能通过彻底的解释来澄清我做错了什么。另外,我的构造函数与测试文件一起工作并且通过了,但只是想知道我的构造函数是否按照问题指定的正确?


import java.util.Arrays;

import java.util.Iterator;


public class SortedArray<T extends Comparable> implements 

java.lang.Iterable<T> {



public SortedArray(int capacity) {


    this.array = (T[]) new Comparable[0];

    this.capacity = capacity;

    this.size = 0;

}



public SortedArray(int capacity, T[] data) {



     if(capacity > data.length)

    {

    this.capacity = capacity;

    }

    else {

            this.capacity = data.length;

            }

    this.size = data.length;


    this.array = (T[]) new Comparable[0];

    }



final public int size() {

            return this.size

}



final public int capacity() {

   return this.capacity;

}



final boolean isEmpty() {       

    return size == 0;

}



final boolean isFull(){                        

    return size == capacity;

}


@Override

final public Iterator<T> iterator() {

    // Do not modify this method.

    return Arrays.stream(array).iterator();

}


// Do not modify these data members.

final private T[] array;     // Storage for the array's element

private int size;      // Current size of the array

final private int capacity;  // Maximum size of the array

}


慕哥6287543
浏览 108回答 1
1回答

哈士奇WWW

您忘记包含“add(T toAdd)”和“remove(T toRemove)”方法,当我尝试让测试通过时,这是绝大多数失败的根源。(注意:失败的痕迹会有所帮助,因为您的添加和删除需要非常复杂才能适应您似乎想要的设计)无论如何,继续修复我所看到的内容。在第二个构造函数中,您实际上从未分配所接收的数据。您调用this.array = (T[]) new Comparable[0];它会创建一个类型为 的空数组Comparable。事实上,您需要打电话this.array = data才能保留给您的东西。另一件事,在您的size()方法中,您忘记在 后放置分号this.size。这往往会阻止事情过去。最后,final private T[] array不能有final,否则你将永远无法添加或删除元素。作为奖励,以下是我用来满足要求并使测试通过的方法(带注释!!!)add():remove()&nbsp; &nbsp; public void add(T t) {&nbsp; &nbsp; &nbsp; &nbsp; if (!(size >= capacity)) { //If there's room...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (size == 0) //If the array is empty...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[0] = t; //Add to first index&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[size] = t; //Add to next available index&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void remove(T element) {&nbsp; &nbsp; &nbsp; &nbsp; if (size <= 0) //If the array is empty...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return; //Stop here&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i <= this.size(); i++) { //Linear search front-to-back&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array[i].equals(element)) { //Find first match&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i] = null; //Delete it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i != size) { //If the match was not at the end of the array...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = i; j <= (this.size() - 1); j++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[j] = array[j + 1]; //Move everything after the match to the left&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return; //Stop here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }附带说明一下,创建SortedArray对象的调用确实应该参数化(使用 <> 例如SortedArray<Integer> arr = new SortedArray<Integer>(5, data);)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java