Reputation: 771
How do you remove from an ArrayList at a particular index then add back to that same index without the removal causing the ArrayList to compensate for the loss of them item and moving the empty space to the end of the array list?
I've tried:
public void dischargePatient(int bedNumber) {
if (bedNumber < beds.size()) {
beds.remove(bedNumber);
}
}
But this moves the bed at bedNumber to the end of the ArrayList after removing the patient from the bed. How do I keep it at bedNumber?
Upvotes: 1
Views: 2462
Reputation: 2778
I know this doesn't answer the question asked, but using a list in such a way, expecting the index to reflect the bed number while also expecting the list to define if a bed is taken is a bit broken, or at least highly troublesome. The examples below show how it is easier to manipulate sets and maps and they provide extra information such as total occupied beds without having to code that up yourself.
I suggest you use a Map of Patients rather than a list.
Map<Integer,Patient> patientBeds = new HashMap<Integer,Patient>();
patientBeds.add(bedNumber, patient);
patientBeds.remove(bedNumber);
patientBeds.contains(bedNumber); // is this bed taken?
patientBeds.size(); // total occupied beds, regardless of their numbers
If you don't need the Patient objects, but simply to know which beds are occupied, use a simple Set of Integers
Set<Integer> set = new HashSet<Integer>();
set.add(bedNumber); // help me nurse
set.remove(bedNumber); // I'm going home!
set.contains(bedNumber); // is this bed taken?
set.size(); // total occupied beds, regardless of their numbers
With either of these solutions, your class which holds the Map or Set might have a member which specifies the total number of available beds in the ward. I wouldn't try to build this concept into the Map such as populating the Map with "null" patients.
int totalBeds = 10;
if (bedNumber > totalBeds) {
// send them to the next ward
}
Upvotes: 0
Reputation: 1025
If you want to keep on using an ArrayList, then you can use the method "set(index, null)" instead of invoking the method "remove(index)". This will ensure that the your order does not change.
Example:
0 1 2 3
ArrayList items: [ A, B, C, D ]
items.set(2, null);
0 1 2 3
items: [ A, B, null, D ]
Upvotes: 0
Reputation: 2995
You want to use a Map object instead. I'm a little rusty at java, but you can define a map of beds like this:
HashMap<Integer,Boolean> occupiedBeds;
and then you can check if an entry is true
to see if a bed is occupied, and then set it to false
when you discharge a patient. You can initialize the occupiedBeds to a range of false
values, or you can just assume an "unset" state means that nobody is in the bed.
Upvotes: 1
Reputation: 1500
You can use beds.get(bedNumber)
and beds.set(bedNumber, patient)
.
Upvotes: 0
Reputation: 393831
You just aspecify the index you want - beds.add(bedNumber, patient);
This pushes the patients that were prior to the addition in locations bedNumber
and later to bedNumber+1 ...., thus bringing your list back to the original order.
public void add(int index, E element)
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
Upvotes: 0