1

我正在用 Java 编写一个程序,该程序采用随机对象的混沌列表,并将学生、教师和教授对象排序到数组列表中,然后打印这些数组列表。

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

}  

抱歉,在适用的情况下格式错误。

4

1 回答 1

0

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

公共类主要{

    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>();
于 2013-05-11T10:16:07.073 回答