Java 在另一个文件中引用内部类时找不到符号
所以我有一个我正在做的项目,我需要为它创建我自己的ArrayList类。我已经定义了所有的方法,但是当我编译时,我的三个方法总是出现“找不到符号”错误。我有一个如下所示的界面:Java 在另一个文件中引用内部类时找不到符号,java,Java,所以我有一个我正在做的项目,我需要为它创建我自己的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
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;
}
}
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
正如你所看到的,我在里面有一个内部类。我试图在实现该接口的另一个文件中使用该类。目前,我的另一个文件中给出问题的三种方法如下:
/**
* 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];
}
}
}
由于
Person
是一个内部类,因此需要使用外部类的名称限定其名称:
protected void expandInsert(int i, FBList.Person person){
FBList.Person[] temp = new FBList.Person[arrayList.length * 2];
...
// and so on...
}
EDIT:删除了将类设置为静态的建议,因为该类嵌套在接口中。对于嵌套在类中的类,该建议是必需的;对于接口,
static
是可选的。为什么Person
是一个内部类?为什么一个人
只能在FBList
的上下文中相关?当我这样做时,我正在设置接口,我的方法需要对人的引用。为什么你不能引用外部人
类?接口不应该被限制在一个类的范围内,当然它们也可以引用其他类?所以我尝试了这个,但是我的编译器输出仍然是这样:FBArrayList.java:56:package FBlist不存在受保护的void insertaden(int I,FBlist.Person){^FBArrayList.java:106:找不到符号符号:方法expandInsert(int,FBList.Person)位置:类FBList.Person[]arrayList.expandInsert(i,Person);^FBArrayList.java:108:找不到符号符号:方法InsertAttend(int,FBList.Person)位置:类FBList.Person[]arrayList.insertattend(i,person);^
很抱歉格式不好。@Dave我想你拼错了FBList
(第三个L
应该是大写)。谢谢,我确实纠正了这一点,但出于某种原因,我仍然得到了与上面相同的输出。:/Dave我在ideone上尝试了这一点。当您将类放在不同的文件中时,请确保将它们声明在同一个包中(也就是说,package
指令(如果有的话)在相关包中的所有文件中都是相同的,并且文件的路径遵循包名的结构。好的,我找到了。我调用了数组本身的方法,而不是使用this。
。它现在可以编译了。谢谢!