因此,我正在處理此項目,並且需要爲它創建自己的ArrayList類。我已經定義了所有的方法,但是當我編譯時,我一直在爲我的三個方法收到「無法找到符號」錯誤。我有一個如下界面:指向另一個文件中的內部類時找不到符號
public interface FBList {
public int size();
public void insert(int i, Person person);
public Person remove(int i);
public Person lookUp(int i);
/**
*A class that defines a person
**/
public class Person {
private String id;
private long phoneNum;
public Person(String personID, long phoneNum){
id = personID;
phoneNum = phoneNum;
}
}
正如你所看到的,我有一個內部類。我試圖在我的其他文件中使用該類實現此接口。目前,有三種方法在我的其他文件給我的問題如下:
/**
* A method to expand the size of the array if the array is too small
* @param i One minus the place in the list where the component will be inserted
* @param Person The person to be put in the list
**/
protected void expandInsert(int i, Person person){
Person[] temp = new Person[arrayList.length * 2];
for(int index = 0; index < temp.length; index++){
if(i != index){
if(i > 0)
temp[index] = arrayList[index];
if(i == 0)
temp[index + 1] = arrayList[index];
}
else{
temp[i] = person;
i = 0;
index--;
}
}
arrayList = temp;
}
/**
* Inserts a new component at the end of a list by creating a new list longer that then last
* @param i The place in the list where the component will be inserted
* @param Person The person to be added to the list
**/
protected void insertAtEnd(int i, Person person){
Person[] temp = new Person[arrayList.length + 5];
for(int index = 0; index < temp.length; index++){
if(index != i){
temp[index] = arrayList[index];
}
else{
temp[index] = person;
}
}
arrayList = temp;
}
/**
* Shrinks the array by one by removing one component from the array
* @param i The index to be removed
**/
protected void shrink(int i){
Person[] temp = new Person[arrayList.length - 1];
for (int index = 0; index < arrayList.length ; index++) {
if (index < i) {
temp[index] = arrayList[index];
}
else if (index == i){
removedPerson = arrayList[index];
temp[index] = arrayList[index + 1];
}
else{
temp[index - 1] = arrayList[index];
}
}
}
所有這些文件都在同一個文件夾,所以不應該出現的問題。我正在使用終端鍵入「javac FBArrayList.java」進行編譯。我的編譯器的輸出是這樣的:
FBArrayList.java:106: cannot find symbol
symbol : method expandInsert(int,FBList.Person)
location: class FBList.Person[]
arrayList.expandInsert(i, person);
^
FBArrayList.java:108: cannot find symbol
symbol : method insertAtEnd(int,FBList.Person)
location: class FBList.Person[]
arrayList.insertAtEnd(i, person);
^
FBArrayList.java:118: cannot find symbol
symbol : method shrink(int)
location: class FBList.Person[]
arrayList.shrink(i);
^
3 errors
爲什麼'Person'是一個內部類?爲什麼'Person'只能在'FBList'的上下文中相關? – 2013-02-10 16:39:36
當我這樣做時,我正在設置我的界面,並且我需要對我的方法使用Person的引用。 – David 2013-02-10 16:48:06
爲什麼你不能提到一個外面的Person類?接口不應該侷限於一個類的範圍,當然它們也可以引用其他類? – 2013-02-10 16:49:34