4

所以我有这个我正在处理的项目,我需要为它创建自己的 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
4

1 回答 1

8

由于Person是一个内部类,它的名字需要用外部类的名字来限定:

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

编辑:删除了创建类的建议,static因为该类嵌套在interface中。对于嵌套在类中的类,该建议是必需的;对于接口,static是可选的。

于 2013-02-10T16:35:21.013 回答