3

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

hank's for reply ! So if i type in this code. The program nows that this is an arraylist that contains Arraylists. So in this place if I write list.add(4) it ads a new list on index 4 ?? What about if I want to "find" the correct arrayList and then add a object to that arraylist ? Once again, thanks
Try it and see. list.add(4) will give you an error, as "4" is not an ArrayList. You can add an ArrayList with list.add(new ArrayList<Integer>()) for example. You can add to a specific index of the ArrayList with list.add(elem,index). Take a look at: docs.oracle.com/javase/7/docs/api/java/util/…, E). The API doc explains the get() method which you want as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.