【发布时间】:2012-10-08 17:49:30
【问题描述】:
我在一个学校项目的嵌套类中遇到了一些麻烦。 目前,我正在尝试编写一种将项目插入到参差不齐的数组数据结构中的方法。 它使用由嵌套类创建的对象来跟踪二维数组位置,以便获取要插入的索引。但是,我在该行收到错误“方法 findEnd(E) 未定义 RaggedArrayList.ListLoc 类型”:
insertLoc.findEnd(item)
我在 stackoverflow 和网络上都进行了广泛搜索,但尚未找到答案。如果我错过了它并且这是多余的(我知道有很多“未定义类型问题的方法”),那么我道歉。
这里是相关代码>>
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;
}
感谢您的任何意见。
【问题讨论】:
-
不要再添加作业标签了。 :) 我们没有使用它。我已经为你删除了。
-
一目了然,我会说这是因为您的方法是私有的,或者范围是错误的(该方法不在正确的位置)。你说该方法不在
ListLoc中,但你是按原样调用它。 -
没有意识到作业标签不再被使用。对不起。感谢您的反馈
标签: java arrays nested-class