2012-10-08 58 views
1

我遇到了學校項目上嵌套類的一些麻煩。 目前,我試圖編寫一個方法來插入一個項目到一個不規則的數組數據結構中。 它使用由嵌套類創建的對象來跟蹤2d數組的位置,以便獲取要插入的索引。不過,我得到錯誤「的方法findEnd(E)是未定義的類型RaggedArrayList.ListLoc」就行了:方法未定義類型爲Java,嵌套類

insertLoc.findEnd(item) 

我已經廣泛既搜查計算器以及圍繞網絡和沒有找到答案了。如果我錯過了它,這是多餘的(有很多「類型問題未定義的方法」,我知道),那麼我很抱歉。

下面是相關代碼>>

嵌套類ListLoc對象:

private class ListLoc { 
    public int level1Index; 
    public int level2Index; 

    public ListLoc() {} 

    public ListLoc(int level1Index, int level2Index) { 
    this.level1Index = level1Index; 
    this.level2Index = level2Index;    
    } 

    public int getLevel1Index() { 
    return level1Index; 
    } 

    public int getLevel2Index() { 
    return level2Index; 
    } 

    // since only used internally, can trust it is comparing 2 ListLoc's 
    public boolean equals(Object otherObj) { 
    return (this == otherObj); 
    } 
} 

方法找到一個匹配的項目(不ListLoc嵌套類的一部分)的最後一個索引:

private ListLoc findEnd(E item){ 
    E itemAtIndex; 

    for (int i = topArray.length -1; i >= 0; i--) { 
    L2Array nextArray = (L2Array)topArray[i]; 

    if (nextArray == null) { 
     continue; 

    } else { 

     for (int j = nextArray.items.length -1; j >= 0; j--) { 
      itemAtIndex = nextArray.items[j]; 
      if (itemAtIndex.equals(item)) { 
       return new ListLoc(i, j+1); 
      } 
     } 
    } 
    } 

    return null; 

}

方法試圖向不規則數組添加新值:

boolean add(E item){ 
    ListLoc insertLoc = new ListLoc(); 
    insertLoc.findEnd(item); 

    int index1 = insertLoc.getLevel1Index(); 
    int index2 = insertLoc.getLevel2Index(); 

    L2Array insertArray = (L2Array)topArray[index1]; 
    insertArray.items[index2] = item; 

    return true; 

}

感謝您的任何輸入。

+0

不要打擾添加作業標籤了。 :)我們沒有使用它。我已經爲你刪除它。 – asteri

+3

好快速瀏覽我會說這是因爲你的方法是私人的,或範圍是錯誤的(方法不在正確的位置)。你說這個方法不在'ListLoc'中,但你是這樣調用的。 – thatidiotguy

+0

沒有意識到作業標籤不再使用。抱歉。感謝您的反饋 –

回答

2

我會打賭,改變這個:

ListLoc insertLoc = new ListLoc(); 
insertLoc.findEnd(item); 

要這樣:

ListLoc insertLoc = findEnd(item); 

將解決您的問題。

您正試圖在ListLoc課程上致電findEnd,但是如果您實際上查看ListLoc,則不會在此處定義它。當您嘗試在insertLoc對象上嘗試呼叫findEnd時,它會失敗,因爲insertLocListLoc的一個實例,我們已經說過,它不包含findEnd

話雖這麼說,我會打賭,findItem在同一類作爲add方法(我們叫它MyList)實際上宣告,所以你想實際調用MyList.findEnd,而不是子虛烏有ListLoc.findEnd

+0

是的。那樣做了。顯然,我還在學習。範圍對我來說依然是一個挑戰。我很感激幫助。 –

+0

您可以點擊打勾接受答案! – CKing

+0

在這種情況下考慮它的一種方法是對象上的''''''''''''''''''''來幫助定義範圍。當你說'insertLoc.findEnd'時,'.'說,「尋找'insertLoc'類('ListLoc'類)中的非靜態方法和字段,併爲'insertLoc'對象調用或訪問它們。 「要訪問_same_類中的方法和字段,請記住我們對'findEnd'的調用與調用'this.findEnd'(嘗試它!)完全相同,因此它也定義了範圍。但是在這裏,變量是'this',它最終給了我們定義'add'的類。希望幫助:) – Brian

0

你幾乎回答了你自己的問題。你試圖在一個ListLoc對象上調用findEnd,但是ListLoc沒有定義一個findEnd方法。你需要或者

一)findLoc添加實施ListLoc或

二)正確的對象上調用findLoc(您沒有提供關於其他類的信息,所以我不能說太多它)

0

方法找到一個匹配的項目(不ListLoc嵌套類的一部分)的最後一個索引:

右鍵 - 這是不是一部分的ListLoc ......但是當你在這裏把它叫做:

ListLoc insertLoc = new ListLoc(); 
insertLoc.findEnd(item); 

...你想調用它,彷彿它是類的一部分。它不是,所以你不能這樣稱呼它。

要麼將​​其移動到ListLoc,要麼改變調用方法的方式。

0

我不知道如果我讀這是正確的,但是從你的解釋看起來像findEnd方法類的外部定義,因此ListLoc確實沒有該名稱的方法...

您的私人ListLoc findEnd(E item)定義在哪裏?

相關問題