ok so i created an array list in the main method and then i made another method in the same class where the main method is and i want to use the information stored in the array in other methods found in another class. how can i do this? here is the class with the array in it and the other class where i want to use the array.
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");
}
}
}
}
this is where i want to use the array public class Database { 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);
}
}
As of now you are just using print(list);
?
In order to pass it to Database
class , You need an instance of that.
As your constructor taking List as a parameter, just try
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.