所以我有我正在研究的这个项目,我需要为它创建自己的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;
}
}
Run Code Online (Sandbox Code Playgroud)
如你所见,我有一个内部阶级.我试图在我的另一个实现此接口的文件中使用该类.目前,我的另一个文件中的三个方法给我的问题如下:
/**
* 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];
}
}
}
Run Code Online (Sandbox Code Playgroud)
所有这些文件都在同一个文件夹中,因此不存在问题.我通过输入"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
Run Code Online (Sandbox Code Playgroud)
由于Person是内部类,因此需要使用外部类的名称限定其名称:
protected void expandInsert(int i, FBList.Person person){
FBList.Person[] temp = new FBList.Person[arrayList.length * 2];
...
// and so on...
}
Run Code Online (Sandbox Code Playgroud)
编辑:删除了建立类的建议,static因为该类嵌套在接口中.嵌套在类中的类需要这个建议; 对于接口,static是可选的.