簡體   English   中英

簡單的Java清單問題

[英]Simple Java List Question

我應該創建一個迭代方法拉伸,將正數n作為參數,並返回一個新的ListItem,開始一個列表,其中原始列表中的每個數字都重復n次。 例如,如果原始列表為(6 7 6 9)並且參數值為2,則返回的新列表為(6 6 7 7 6 6 9 9)。

我已經有一個listitem構造函數,該構造函數具有該節點的值和對下一個節點的引用:

   ListItem(int number, ListItem next) {
        this.number = number;
        this.next   = next;
}

我的代碼如下所示:

    public ListItem stretch(int n) {
        //make an array of list items that is n times bigger than the original one.
        ListItem[] newList = new ListItem[this.length() * n];

    //Then loop through the old list one value at a time. At each value do a second loop n times to stretch

        int index = 0;
        int counter = 0;
        for(int i = 0; i < this.length(); i++){
            while(counter++ < n){
                newList[index++] = this[i];*************************
        }
        return newList;****************
    }

}

有兩個問題要點,在這些問題上我加了注視。 我應該返回一個listitem,但是NewList是一個數組。 我不確定第一條加星標的行有什么問題。

任何幫助/指導將不勝感激。

解決此問題的最佳方法是繪制一些圖片。 然后嘗試將問題分解為多個子問題。 讓我們從一個簡單的案例開始:一個長度為2的列表:

ListItem two = new ListItem(1, ListItem(2, null));

這是一張照片

two = ( number == 1
      ( next   == ( number == 2
                  ( next   == null

這是另一張圖片:

+---+  +---+    The "/" here is the "null" above, which terminates the list.
| 1 |->| 2 |-/  
+---+  +---+

這樣想:一個列表由第一個ListItem組成,它通過“ next”指向列表的其余部分。 空列表則為null,最后一個ListItem的“ next”始終為空。 (空值)。

現在,當我們被要求“拉伸”列表時,實際上發生了什么? 說2點?

好了,空列表很容易,它不會改變。 但這也無關緊要,因為null.stretch()會以您使用的語言結尾很差。 那么長度1的列表就是我們最簡單的實際情況:

我們有:

we have       we want

+---+         +---+   +---+
| 1 |-/       | 1 |-->| 1 |-/
+----         +---+   +---+

好吧,這並不難。 我們已經有一個長度為1的列表。 我們需要做的就是將它掛在新ListItem的下一個,我們將得到一個長度為2的列表。 顯然,我們需要能夠向現有列表添加內容的功能。 將其添加到最容易的位置,因此我們將為此定義一個小幫手:

ListItem addItemToFront(int number) {
    return new ListItem(number, this);
} 

好的,現在讓我們對其進行編碼,並將其稱為stretchFirstItemByOne:

ListItem stretchFirstItemByOne() {           
    return this.addItemToFront(this.number); 
}

在這些示例中,您會看到我經常使用this.something(),盡管這不是必需的。 我只是想弄清楚這些是當前對象(此)上的方法調用。

但是,假設我們要延伸更大的n 您已經嘗試過-有點不幸-在上面使用了for循環。 你可以那樣做。 但是我將以不同的方式來做。

ListItem stretchFirstItem(n) {
    if (n == 1)      // stretching to length 1 means nothing
        return this; // to do. just return this.
    else {
        // well, if we stretch our item to length n-1 first
        // then all we have to do is stretch it by one and
        // we're done.
        return this.stretchFirstItem(n-1).stretchFirstItemByOne(); 
    }
}

停下來想一想。 如果遇到問題,請將其重寫為for循環。

您可能會說,這非常好,但是它只能處理長度為1的列表。 多么真實,多么真實。

假設您有一個長度為3的列表,並且想將其擴展為2。

+---+  +---+  +---+
( | 1 |->| 2 |->| 3 |-/ ).stretch(2)
  +---+  +---+  +---+

強硬? 好吧,至少我們可以開始。 如果列表只有一項,我們知道如何處理:

ListItem stretch(int n) {
    ListItem restOfList = this.next;
    if (restOfList == null) { // this list has length one
        return this.stretchFirstItem(n);
    } else {
        // if we had the rest of the list stretched, then we could
        // add this.number to the front of this stretched list, stretch
        // that first item and then we'd be done.
    }
}

嘿,但不舒展應該這樣做對我們來說,你知道,舒展整個名單? 我們不能使用它來拉伸列表的其余部分,以便我們可以輕松地拉伸第一項嗎? 但是我們甚至還沒有完成拉伸的工作-我的意思是-它沒有用。 不可能那么容易,對嗎? 可以嗎

ListItem stretch(int n) {
    ListItem restOfList = this.next;
    if (restOfList == null) { // this list has length one
        return this.stretchFirstItem(n);
    } else {
        return restOfList     //-------------------------
           .magic(...)        // Left as an exercise for
           .moreMagic(...)    // the reader.
           .zyzzy(...);       //-------------------------
    }
}

家庭作業?

看來您應該建立一個鏈表,但是您要建立一個ListItems數組。 ListItems的數組不錯 ,但是您需要使每個ListItem的下一個值指向列表中的下一個項目。

然后,函數的最后一行將返回列表中的第一項。

第一行加星號肯定是錯誤的,但是如果您考慮一下,那不是您的問題:首先不要求您返回數組。

如果考慮輸入內容的實際情況,如何返回ListItem的問題應自行解決:拿一張紙,畫一下,您將了解為什么返回一個列表項就足夠了,以及如何處理列表項。 ListItem使每個項目加倍。

第一行的問題在於,由於this不是數組,因此無法下標( [i] )。 如果沒有有關ListItem類的更多詳細信息,我將無法幫助您解決該問題。 您在制作鏈表嗎?

對於返回,您可能想返回數組中的第一項。 如果是這樣,請將其更改為return newList[0] 如果要返回數組,請更改函數以返回數組,如下所示: public ListItem[] stretch(

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM