我遇到了學校項目上嵌套類的一些麻煩。 目前,我試圖編寫一個方法來插入一個項目到一個不規則的數組數據結構中。 它使用由嵌套類創建的對象來跟蹤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;
}
感謝您的任何輸入。
不要打擾添加作業標籤了。 :)我們沒有使用它。我已經爲你刪除它。 – asteri
好快速瀏覽我會說這是因爲你的方法是私人的,或範圍是錯誤的(方法不在正確的位置)。你說這個方法不在'ListLoc'中,但你是這樣調用的。 – thatidiotguy
沒有意識到作業標籤不再使用。抱歉。感謝您的反饋 –