2
제가 해결하려고하는 문제가 있습니다. 알 수없는 요소 수의 ArrayList를 만들고 그 ArrayList에 다른 ArrayList를 배치하려고합니다. 예 : 병원 대기열.이 대기열에는 환자의 우선 순위에 따라 다른 대기열이 있어야합니다. ArrayList에 내 ArrayList에있는을위한ArrayList 내에서 (알 수없는) 개수의 ArrayList를 만드는 방법은 무엇입니까?
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)
}
행크는 대답했습니다! 그래서이 코드를 입력하면됩니다. 이 프로그램은 Arraylists를 포함하는 arraylist임을 나타냅니다. 그래서이 곳에서 list.add (4)라고 쓰면 인덱스 4에 새 목록이 표시됩니까 ?? 올바른 arrayList를 "찾고"객체를 arraylist에 추가하려면 어떻게해야합니까? 다시 한번 감사 드리며 – user3187178
시도해보십시오. "4"는 ArrayList가 아니므로 list.add (4)는 오류를 발생시킵니다. 예를 들어 list.add (new ArrayList())와 함께 ArrayList를 추가 할 수 있습니다. list.add (elem, index)를 사용하여 ArrayList의 특정 인덱스에 추가 할 수 있습니다. http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add(int, E)에서 살펴보십시오. API 문서에는 원하는 get() 메소드도 설명되어 있습니다. –