user3187178
user3187178

Reputation: 31

How to create a (unknown) number of ArrayLists within an ArrayList?

I have a problem that I'm trying to Solve. I want to create an ArrayList of unknown number of elements, and in that ArrayList place other ArrayLists. For example: A hospital queue, and within this queue I want to have different queues based in the priority of the patient.

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

Upvotes: 3

Views: 562

Answers (1)

Martin Dinov
Martin Dinov

Reputation: 8825

For having an ArrayList within an ArrayList, just do:

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

That'll give you an ArrayList of ArrayLists of type Type. You can then just add ArrayList's to list as you would add to an ArrayList normally. You don't have to know the number of elements an ArrayList will hold in advance.

Upvotes: 3

Related Questions