亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據測試文件實施方法時出現的問題

根據測試文件實施方法時出現的問題

慕哥6287543 2023-07-19 17:30:57
我能夠使構造函數和容量方法起作用,但不知道為什么 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;}@Overridefinal 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 elementprivate int size;      // Current size of the arrayfinal private int capacity;  // Maximum size of the array}
查看完整描述

1 回答

?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

您忘記包含“add(T toAdd)”和“remove(T toRemove)”方法,當我嘗試讓測試通過時,這是絕大多數失敗的根源。(注意:失敗的痕跡會有所幫助,因為您的添加和刪除需要非常復雜才能適應您似乎想要的設計)


無論如何,繼續修復我所看到的內容。


在第二個構造函數中,您實際上從未分配所接收的數據。您調用this.array = (T[]) new Comparable[0];它會創建一個類型為 的空數組Comparable。事實上,您需要打電話this.array = data才能保留給您的東西。


另一件事,在您的size()方法中,您忘記在 后放置分號this.size。這往往會阻止事情過去。


最后,final private T[] array不能有final,否則你將永遠無法添加或刪除元素。


作為獎勵,以下是我用來滿足要求并使測試通過的方法(帶注釋?。。。゛dd():remove()


    public void add(T t) {

        if (!(size >= capacity)) { //If there's room...

            if (size == 0) //If the array is empty...

                array[0] = t; //Add to first index

            else

                array[size] = t; //Add to next available index

            size++;

        }

    }


    public void remove(T element) {

        if (size <= 0) //If the array is empty...

            return; //Stop here

        else {

            for (int i = 0; i <= this.size(); i++) { //Linear search front-to-back

                if (array[i].equals(element)) { //Find first match

                    array[i] = null; //Delete it

                    size--;

                    if (i != size) { //If the match was not at the end of the array...

                        for (int j = i; j <= (this.size() - 1); j++) 

                            array[j] = array[j + 1]; //Move everything after the match to the left

                    }

                    return; //Stop here

                }

            }

        }

    }

附帶說明一下,創建SortedArray對象的調用確實應該參數化(使用 <> 例如SortedArray<Integer> arr = new SortedArray<Integer>(5, data);)。


查看完整回答
反對 回復 2023-07-19
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號