How to access objects inside ArrayList common ArrayLists templates

I am new to Java and even new to generics in Java. I was looking for similar questions, but could not find a direct answer for my specific problem.

I am developing a project for managing patients, doctors, consultations, medical events and everything related to medical clinics. What I'm trying to do right now is to create a list of medical events related to each patient. To this list of medical events at the moment, only admission to exams and prescriptions is allowed, but it must be extensible: I want to be able to add other types of medical events in the future, if I need it, such as information about operations.

So, I started by creating an ArrayList from generic ArrayLists in the Patient class, with its type limited by the extension of the MedicalEvent class (so at the moment it's an ArrayList from ArrayLists of type Prescription or Exam). I also created an ArrayList of type Prescription and another type of Exam.

List<ArrayList<? extends MedicalEvent>> medicalevents;

private ArrayList<Prescription> prescriptions;

private ArrayList<Exam> exams;

Then in the constructor I added ArrayLists recipes and exams to ArrayList medicines.

medicalevents.add(prescriptions);

medicalevents.add(exams);

To add medical events to one of two valid types, I defined the following method:

 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); } } 

The problem is that I get the error message "The add (capture # 1-of? Extends MedicalEvent) method in the ArrayList type is not applicable for (Prescription) arguments" and I don't know what that means. Can someone tell me what I'm doing wrong, or suggest a better way to solve this problem?

Thanks!

+4
source share
2 answers

Given the following announcements

 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 } } 

you cannot add B to as , because it will cause a problem later when you try to read from the list:

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

To better understand this problem, a ridiculous example:

 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(); 

Typically, collections with restricted wildcards of type List<? extends Something> List<? extends Something> are useful for code that will not modify the collection, but simply iterate over it, doing something with the elements.

As for the original problem - you can change the code, so there are two different lists: one for Prescription , the other for Exam . You can still have only one method that will iterate over these two lists, doing something useful (for example, print their contents):

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

Would something like this work better?

 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; } } } 
+1
source

All Articles