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

时间:2014-01-12 12:05:57

标签: java arraylist

我有一个问题,我正在努力解决。我想创建一个未知数量的元素的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)
}

1 个答案:

答案 0 :(得分:2)

要在ArrayList中使用ArrayList,只需执行:

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

那将为您提供Type类型的ArrayLists的ArrayList。然后,您可以将ArrayList添加到list,就像通常添加到ArrayList一样。您不必事先知道ArrayList将保留的元素数量。