2013-02-10 23 views
2

因此,我正在處理此項目,並且需要爲它創建自己的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 
+1

爲什麼'Person'是一個內部類?爲什麼'Person'只能在'FBList'的上下文中相關? – 2013-02-10 16:39:36

+0

當我這樣做時,我正在設置我的界面,並且我需要對我的方法使用Person的引用。 – David 2013-02-10 16:48:06

+0

爲什麼你不能提到一個外面的Person類?接口不應該侷限於一個類的範圍,當然它們也可以引用其他類? – 2013-02-10 16:49:34

回答

7

由於Person是一個內部類,它的名字必須是合格與外部類的名稱:

protected void expandInsert(int i, FBList.Person person){ 
    FBList.Person[] temp = new FBList.Person[arrayList.length * 2]; 
    ... 
    // and so on... 
} 

編輯:刪除一個因爲該類嵌套在接口內,所以建議使用類static。嵌套在類內部的類需要這個建議;對於接口,static是可選的。

+0

所以,我想這一點,但我的編譯器的輸出仍然是這樣的:'FBArrayList.java:56:包FBlist不存在 \t保護無效insertAtEnd(INT I,FBlist.Person人){ \t^ FBArrayList.java: 106:找不到符號 symbol:方法expandInsert(int,FBList.Person) location:class FBList.Person [] \t \t \t arrayList.expandInsert(i,person); \t \t \t^ FBArrayList.java:108:找不到符號 符號:方法insertAtEnd(INT,FBList.Person) 位置:類FBList.Person [] \t \t \t arrayList.insertAtEnd(I,人); \t \t \t^ '對不好的格式。 – David 2013-02-10 16:56:29

+0

@Dave我認爲你拼寫錯誤'FBList'(第三個'L'應該是大寫)。 – dasblinkenlight 2013-02-10 17:06:04

+0

謝謝,我確實糾正了這一點,但由於某種原因,我仍然得到了與上述相同的輸出。 :/ – David 2013-02-10 17:13:05

相關問題