如何迭代不同对象的arraylist

时间:2015-10-08 14:57:21

标签: java collections

ArrayList我有两个不同的对象, StudentEmployee。我想逐个遍历它们。我能够遍历列表并使用Employee对象而不是Student对象。

我有以下代码:

package javaCollections;

import java.util.ArrayList;
import java.util.Iterator;

class Employee {

    @Override
    public String toString() {
        return "employee [name=" + name + ", age=" + age + "]";
    }

    public String name;
    public int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    Employee(String name, int age) {
        this.age = age;
        this.name = name;
    }

}

class Student {

    @Override
    public String toString() {
        return "student [stud_name=" + stud_name + ", rollNumber=" + rollNumber
                + "]";
    }

    String stud_name;
    int rollNumber;

    public Student(String stud_name, int rollNumber) {
        super();
        this.stud_name = stud_name;
        this.rollNumber = rollNumber;
    }

    public String getStud_name() {
        return stud_name;
    }

    public void setStud_name(String stud_name) {
        this.stud_name = stud_name;
    }

    public int getRollNumber() {
        return rollNumber;
    }

    public void setRollNumber(int rollNumber) {
        this.rollNumber = rollNumber;
    }
}

public class Arraylist {

    ArrayList<Object> emparray;

    public void addemp() {
        Employee emp = new Employee("abc", 12);
        emparray = new ArrayList<Object>();
        emparray.add(emp);
        Employee emp1 = new Employee("def", 12);
        emparray.add(emp1);

        Student std = new Student("efg", 123);
        Student std1 = new Student("xyz", 123);
        emparray.add(std);
        emparray.add(std1);

    }

    public void iterateemp() {
        /*
         * Iterator<Object> itr=emparray.iterator();
         * 
         * while(itr.hasNext()) { System.out.println(itr.next()); }
         */

        for (Object e : emparray) {
            System.out.println(((Employee) e).getAge());
            System.out.println(((Employee) e).getName());
        }
    }

    public static void main(String[] args) {
        Arraylist al = new arraylist();
        al.addemp();
        al.iterateemp();
    }

}

有人可以帮我这个吗?

3 个答案:

答案 0 :(得分:6)

您需要做的是检查对象的实例。

for (Object e : emparray) {
        if(e instanceof employee) {
            System.out.println(((employee) e).getAge());
            System.out.println(((employee) e).getName());
        } else if(e instanceof student) {
            // do something else
        }
    }
}

IMO这是一个糟糕的设计。

答案 1 :(得分:4)

最佳做法是创建名为Person的公共基础,其共享字段为name。然后,您可以在循环中将Object替换为Person

import java.util.ArrayList;
import java.util.Iterator;



interface Person{

     public String getName();
     public void setName(String name);
}

class employee implements Person{


    @Override
    public String toString() {
        return "employee [name=" + name + ", age=" + age + "]";
    }

    public String name;
    public int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    employee(String name, int age) {
        this.age = age;
        this.name = name;
    }

}

class student implements Person{

    @Override
    public String toString() {
        return "student [stud_name=" + name + ", rollNumber=" + rollNumber
                + "]";
    }

    String name;
    int rollNumber;

    public student(String stud_name, int rollNumber) {
        super();
        this.name = stud_name;
        this.rollNumber = rollNumber;
    }


    public int getRollNumber() {
        return rollNumber;
    }

    public void setRollNumber(int rollNumber) {
        this.rollNumber = rollNumber;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name=name;
    }
}

public class arraylist {

    ArrayList<Person> emparray;

    public void addemp() {
        employee emp = new employee("abc", 12);
        emparray = new ArrayList<Person>();
        emparray.add(emp);
        employee emp1 = new employee("def", 12);
        emparray.add(emp1);

        student std = new student("efg", 123);
        student std1 = new student("xyz", 123);
        emparray.add(std);
        emparray.add(std1);

    }

    public void iterateemp() {
        for (Person e : emparray) {
            if (e instanceof employee) {
                System.out.println(((employee) e).getAge());    
            }else{
               /// do for student   
            }
            System.out.println(e.getName());
        }
    }

    public static void main(String[] args) {
        arraylist al = new arraylist();
        al.addemp();
        al.iterateemp();
    }

}

答案 2 :(得分:3)

for (Object e : emparray) {
        if(e instanceof employee) {
            System.out.println(((employee) e).getAge());
            System.out.println(((employee) e).getName());
        } else if(e instanceof student) {
            System.out.println(((student) e).getRollNumber());
            System.out.println(((student) e).getStud_name());
        }
    }
}