2013-05-11 174 views
1

我正在编写一个Java程序,它使用随机对象的混沌列表并将学生,教师和教授对象排列为arraylist,然后打印这些arrayLists。Java错误:无法找到符号(当用add调用arrayList时)

一切都工作(所以我不会在这里发布其他类文件),除非我试图从某个地方调用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); 
} 

} 

很抱歉的坏格式适用。

+0

你'ArrayList'已知仅在'main'方法的范围。它是'ArrayList',而不是'arrayList'。 – Maroun 2013-05-11 10:14:02

+2

此外,类型区分大小写 - 'arrayList'!='ArrayList' – 2013-05-11 10:15:15

回答

0

你的代码似乎有两个问题。首先你需要使用ArrayList而不是arrayList。当你在本地方法中定义它时,你似乎试图访问不同方法中的Arraylists。如果在方法中定义了一个变量,则无法在其他方法中访问该变量。因此,您可能需要将全局数组列表定义为您的类变量,而不是您的主函数。下面是所需的代码变化:

公共类主要{

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>(); 
+0

我在main()现在和现在它的工作前定义了ArrayList作为静态ArrayList(带有大写A)。谢谢! – raptor777777 2013-05-11 11:39:26