如何访问通用ArrayLists的ArrayList中的对象

时间:2012-12-01 15:16:07

标签: java generics arraylist

我是Java的新手,甚至是Java中的泛型。我已经找到了类似的问题,但没有找到解决我特定问题的直接答案。

我正在开发一个项目来管理患者,医生,咨询,医疗事件以及与医疗诊所相关的所有事情。 我现在要做的是创建一个与每位患者相关的医疗事件清单。对于这个医疗事件列表,目前只允许添加考试和处方,但它应该是可扩展的:如果我需要,我希望将来能够添加其他类型的医疗事件,例如有关手术的信息。

因此,我首先在Patient类中创建了一个通用ArrayLists的ArrayList,其类型有义将由MedicalEvent类扩展(因此,现在,它是Prescription或Exam类型的ArrayLists的ArrayList)。我还创建了一个Prescription类型的ArrayList和另一个类型Exam。

List<ArrayList<? extends MedicalEvent>> medicalevents;

private ArrayList<Prescription> prescriptions;

private ArrayList<Exam> exams;

然后,在构造函数中,我将ArrayLists处方和检查添加到ArrayList医疗事件中。

medicalevents.add(prescriptions);

medicalevents.add(exams);

为了添加两种允许类型之一的医疗事件,我定义了以下方法:

public void addMedicalEvent(E element){

if(element instanceof Prescription){
    medicalevents.get(0).add((Prescription)element);
    }
if(element instanceof Exam){
    medicalevents.get(1).add((Exam)element);
    }
}

问题是,我得到错误“方法添加(捕获#1-of?extends MedicalEvent)类型ArrayList不适用于参数(处方)”,我不知道它的含义。谁能告诉我我做错了什么,或建议更好的方法来解决这个问题?

谢谢!

2 个答案:

答案 0 :(得分:7)

给出以下声明

class A {}
class B extends A {}
class C extends A {}


public class SOSample {
    public static void main(String[] args) {
        List<List<? extends A>> list = new ArrayList<List<? extends A>>();
        final List<? extends A> as = list.get(0);
        as.add(new B()); // error here
    }
}

您无法将B添加到as,因为稍后当您尝试从列表中读取时会导致问题:

A a = list.get(0).get(0); // is a B or C?

为了更好地理解这个问题,有一个有趣的例子:

class Vehicle { void launch(); }
class Car extends Vehicle {}
class NuclearMissile extends Vehicle {}
...
// this is prohibited because of below code
List<? extends Vehicle> cars = new ...
// imagine this is possible...
cars.add(new NuclearMissile());
// now this is possible
cars.get(0).launch();

通常,带有有界通配符(如List<? extends Something>)的集合对于不会修改集合的代码很有用,但只是迭代它对元素执行某些操作。

关于您的原始问题 - 您可以更改代码,因此有两个不同的列表,一个用于Prescription,另一个用于Exam。你仍然可以只使用一个方法来迭代这两个列表做一些有用的事情(比如打印他们的内容):

void doSomethingWithEvents(List<? extends Event> events) {
}

答案 1 :(得分:1)

这样的事情会更好吗?

class Medical {
    List<EventList<?>> eventLists = new ArrayList<EventList<?>>();

    Medical() {
        eventLists.add(new EventList<Perscription>(Perscription.class));
        eventLists.add(new EventList<Exam>(Exam.class));
    }

    boolean add(Object item) {
        for(EventList<?> list : eventLists) {
            if(list.tryToAdd(item)) {
                return true;
            }
        }
        return false;
    }
}

class EventList<T> {
    Class<T> type;
    List<T> list = new ArrayList<T>();

    EventList(Class<T> type) {
        this.type = type;
    }

    boolean tryToAdd(Object item) {
        if(type.isInstance(item)) {
            list.add(type.cast(item));
            return true;
        } else {
            return false;
        }
    }
}