I’m running into some trouble with nested classes on a project for school.
Currently, I’m trying to write a method to insert an item into a ragged array data structure.
It uses an object created by a nested class to keep track of the 2d array location in order to get the indexes to insert at. However, I am getting the error “The method findEnd(E) is undefined for the type RaggedArrayList.ListLoc” on the line:
insertLoc.findEnd(item)
I have searched extensively both on stackoverflow as well as around the web and have not found the answer yet. If I have missed it and this is redundant (there are a lot of “method undefined for type questions”, I know) then I apologize.
Here is the relevant code >>
nested class for ListLoc object:
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);
}
}
Method to find the last index of a matching item (not part of ListLoc nested class):
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;
}
Method attempting to add a new value to the ragged array:
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;
}
Thanks for any input.
I’m going to bet that changing this:
To this:
Will fix your problem.
You’re trying to call
findEndon theListLocclass, but if you actually look atListLoc, it’s not defined there. When you tried to callfindEndon theinsertLocobject, it fails becauseinsertLocis an instance ofListLoc, which we already said doesn’t containfindEnd.That being said, I’m going to bet that
findItemis actually declared in the same class as theaddmethod (let’s just call itMyList), so you wanted to actually callMyList.findEnd, not the non-existentListLoc.findEnd.