我正在用Java语言写一个程序,它获取一个包含随机对象的混乱列表,并将学生、教师和教授对象排序到数组列表中,然后打印这些arraylists。
一切正常(所以我不会在这里发布其他类文件),除非当我试图从某个地方调用arrayLists时,它会显示“错误:找不到符号”。我在堆栈溢出的其他地方找不到解决问题的方法。
代码如下:
import java.util.ArrayList;
public class Main {
public static void main(String[] argv) {
arrayList<Object> onlyTeachers = new ArrayList<Object>();
arrayList<Object> onlyStudents = new ArrayList<Object>();
arrayList<Object> onlyProfessors = new ArrayList<Object>();
// Do not change below
ArrayList<Object> chaos = new ArrayList<Object>();
chaos.add(new Teacher("deWitt, Booker", "Collar Avenue 45", 2222));
chaos.add(new Student("Johnsen, John", "the Road 6", 1231231));
chaos.add(new Student("Pun, Peter", "Applestreet 4", 1234));
chaos.add(new Boolean(false));
chaos.add(null);
chaos.add(new Teacher("Wiering, John", "Puppetlane 1", 7979786));
chaos.add(new Student("Cheese, Anna", "Cheesemarket 1", 455656));
chaos.add(new Teacher("White, Snow", "Fairy tale lane 3", 7889867));
chaos.add(new Student( "Peterson, Peter", "Canalstreet 3", 8998));
chaos.add(new Professor("dr.","Manson, Derrick", "Zakuroad 124", 899844));
chaos.add(new Student("Whiskers, Hettie", "Anotherroad 3", 9123));
chaos.add(new Double(10));
chaos.add(new Student("deGroot, Lambert", "Chirplane 2", 89444498));
chaos.add(new Professor("BSc.", "Pan, Peter", "Swinkelroad 2", 892438));
chaos.add(new Student("Bali, Ali", "Aroundthecorner 662", 8923498));
chaos.add(new Integer(12));
chaos.add(new Teacher("Benson, Ben", "Somewhere 25", 8963298));
chaos.add(new Student("Youssouf, Mohammed", "There 17", 89364698));
// Do not change the above
for(Object x : chaos){
cleanUp(x);
}
System.out.println("\n\n\n");
for(Object x : onlyProfessors){
System.out.println(x);
System.out.println("\n");
}
System.out.println("\n\n\n");
for(Object x : onlyStudents){
System.out.println(x);
System.out.println("\n");
}
System.out.println("\n\n\n");
for(Object x : onlyTeachers){
System.out.println(x);
System.out.println("\n");
}
}
public static void cleanUp(Object object){
if(object instanceof Person){
if(object instanceof Professor){
System.out.println("\nThis is a professor.");
onlyProfessors.add(object);
} else {
if(object instanceof Student){
System.out.println("\nThis is a student.");
onlyStudents.add(object);
} else {
if(object instanceof Teacher){
System.out.println("\nThis is a Teacher.");
onlyTeachers.add(object);
}
}
}
} else {
System.out.println("\nThis is not a person.");
}
System.out.println(object);
}
}
对于适用的错误格式,我深表歉意。
发布于 2013-05-11 18:16:07
您的代码中似乎存在两个问题。首先,您需要使用arrayList而不是ArrayList。而且,当Arraylist在本地的方法中定义时,您似乎正在尝试以不同的方法访问Arraylist。如果变量是在方法中定义的,则不能在其他方法中访问它。因此,您可能需要将数组列表全局定义为类变量,而不是在main函数中定义。以下是所需的代码更改:
公共类Main {
static ArrayList<Object> onlyTeachers = new ArrayList<Object>();
static ArrayList<Object> onlyStudents = new ArrayList<Object>();
static ArrayList<Object> onlyProfessors = new ArrayList<Object>();
public static void main(String[] argv) {
// remove the list from here
// arrayList<Object> onlyTeachers = new ArrayList<Object>();
// arrayList<Object> onlyStudents = new ArrayList<Object>();
// arrayList<Object> onlyProfessors = new ArrayList<Object>();https://stackoverflow.com/questions/16495984
复制相似问题