第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

根據(jù)測(cè)試文件實(shí)施方法時(shí)出現(xiàn)的問(wèn)題

根據(jù)測(cè)試文件實(shí)施方法時(shí)出現(xiàn)的問(wèn)題

慕哥6287543 2023-07-19 17:30:57
我能夠使構(gòu)造函數(shù)和容量方法起作用,但不知道為什么 size()、isFull() 和 isEmpty() 失敗。我相信它非常簡(jiǎn)單,但我只是無(wú)法看到一個(gè)小錯(cuò)誤并修復(fù)它。希望有人能通過(guò)徹底的解釋來(lái)澄清我做錯(cuò)了什么。另外,我的構(gòu)造函數(shù)與測(cè)試文件一起工作并且通過(guò)了,但只是想知道我的構(gòu)造函數(shù)是否按照問(wèn)題指定的正確?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貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊

您忘記包含“add(T toAdd)”和“remove(T toRemove)”方法,當(dāng)我嘗試讓測(cè)試通過(guò)時(shí),這是絕大多數(shù)失敗的根源。(注意:失敗的痕跡會(huì)有所幫助,因?yàn)槟奶砑雍蛣h除需要非常復(fù)雜才能適應(yīng)您似乎想要的設(shè)計(jì))


無(wú)論如何,繼續(xù)修復(fù)我所看到的內(nèi)容。


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


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


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


作為獎(jiǎng)勵(lì),以下是我用來(lái)滿足要求并使測(cè)試通過(guò)的方法(帶注釋!?。。゛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

                }

            }

        }

    }

附帶說(shuō)明一下,創(chuàng)建SortedArray對(duì)象的調(diào)用確實(shí)應(yīng)該參數(shù)化(使用 <> 例如SortedArray<Integer> arr = new SortedArray<Integer>(5, data);)。


查看完整回答
反對(duì) 回復(fù) 2023-07-19
  • 1 回答
  • 0 關(guān)注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)