如何在 ArrayList 中创建(未知)数量的 ArrayLists



我有一个问题,我正在尝试解决。我想创建一个未知数量元素的 ArrayList,并在该 ArrayList 中放置其他 ArrayLists。例如:一个医院队列,在这个队列中,我希望根据患者的优先级有不同的队列。

System.out.println("whats the maximum priority?");
int maxPriority = Scan.nextInt();
//Trying to create a new ArrayList in every ArrayList index.
for(int k=0; k<maxPriority;k++){
    queues[k] = new Arraylist();
}
//trying to create a "patient"-object with a name and priority
public static patient(String name, int priority){
    this.name=name;
    this.priority=priority;
}       
// trying to get the priority of a patient//
public static getPriority(){
    return priority;
}
// Trying to add the patient last in the correct "priority queue"
public static placePatient()){
    queues.add(patient.getPriority)
}
要在 ArrayList

中拥有一个 ArrayList,只需执行以下操作:

ArrayList<ArrayList<Type>> list = new ArrayList<ArrayList<Type>>();

这将为您提供类型类型的 ArrayList of ArrayLists。然后,您可以将 ArrayList 添加到list就像正常添加到 ArrayList 一样。您不必事先知道 ArrayList 将包含的元素数量。

最新更新