如何将main方法中创建的ArrayList传递给另一个类中的另一个方法

时间:2014-04-23 05:13:02

标签: java arraylist

好吧所以我在main方法中创建了一个数组列表然后我在main方法所在的同一个类中创建了另一个方法,我想在另一个类中找到的其他方法中使用存储在数组中的信息。我怎样才能做到这一点?这里是包含数组的类,另一个是我想要使用数组的类。

    public class Driver {

     public static void main(String[] args){


    Address st1 = new Address("9215", "", "San Pedro ST", "Los Angeles", "CA", "90003");
    Address st2 = new Address("6015", "1/2", "Hooper Ave", "Los Angeles", "CA", "90001");
    Address fac1 = new Address("8012", "", "Alameda Ave", "Los Angeles", "CA", "90002");
    Address fac2 = new Address("8145", "", "flower St", "Los Angeles", "CA", "90023");
    Address stff1 = new Address("1256", "", "main", "culver city", "CA", "70231");
    Address stff2 = new Address("9756", "", "florence Ave", "Los Angeles", "CA", "90001");

    PhoneNumber stph1 = new PhoneNumber("home phone", "323", "586", "4569");
    PhoneNumber stph2 = new PhoneNumber("home phone", "213", "758", "1563");
    PhoneNumber facph1 = new PhoneNumber("home phone", "313", "752", "5623");
    PhoneNumber facph2 = new PhoneNumber("home phone", "323", "578", "4521");
    PhoneNumber stffph1 = new PhoneNumber("home phone", "323", "755", "1256");
    PhoneNumber stffph2 = new PhoneNumber("home phone", "213", "756", "8541");

    Student student1 = new Student("junior", "Victor", "Guerrero", "vic@me.com", st1, stph1);
    Student student2 = new Student("freshman", "juanr", "Guerro", "jusn@me.com", st2, stph2);

    Faculty faculty1 = new Faculty("10-12", "full time", "salazar hall", 15, "mike", "jones", "jone@me.com", fac1, facph1);
    Faculty faculty2 = new Faculty("11-1", "full time", "et", 15, "minor", "sepeda", "minor@me.com", fac2, facph2);

    Staff staff1 = new Staff("chair man", "et", 15, "minor", "sepeda", "minor@me.com", stff1, stffph1);
    Staff staff2 = new Staff("supervisor", "musc hall", 15, "steven", "x", "x@me.com", stff2, stffph2);

    ArrayList<Person> list = new ArrayList<Person>();
    list.add(student1);
    list.add(student2);
    list.add(faculty1);
    list.add(faculty2);
    list.add(staff1);
    list.add(staff2);
    print(list);
}

public static void print(ArrayList<Person> list) {


    for (Person o : list) {
        if (o instanceof Student) {
            System.out.println("Student: " + ((Student)o).toString() + "\n");
        }
        else if (o instanceof Faculty) {
            Faculty f = (Faculty)o;
            System.out.println("Faculty: " + f.toString()+ "\n");
        }
        else if (o instanceof Staff){
            Staff fa = (Staff)o;
            System.out.println("Staff: " + fa.toString()+ "\n");
        }
    }
    }

 }

这是我想要使用数组的地方           公共类数据库{     private ArrayList list;

public Database() {

}


public Database(ArrayList<Person> list){
    this.list = list;



}
        public void printDatabase(String type){
    if (type.equals("Student")){
        for (int i = 0; i < list.size(); i++){
        if (list.equals("freshman")){
        System.out.println(list.get(i));
        }
        else if (type.equals("Faculty")){
        System.out.println(list.get(i));
        }
        else if (type.equals("Staff")){
            System.out.println(list.get(i));
            }
    }
    }


      }

     public void printDatabase() {
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list);
    }

}

1 个答案:

答案 0 :(得分:0)

截至目前,您只是使用print(list);

为了将它传递给Database类,你需要一个实例。

作为构造函数将List作为参数,只需尝试

ArrayList<Person> list = new ArrayList<Person>();
    list.add(student1);
    list.add(student2);
    list.add(faculty1);
    list.add(faculty2);
    list.add(staff1);
    list.add(staff2);
    Database  db = new Database(list);
    db.printDatabase();
    db.printDatabase("Student"); // printing only students.