简体   繁体   English

如何从ArrayList中删除项目,然后添加回删除位置?

[英]How to remove item from ArrayList, then add back to place of removal?

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? 如何从特定索引的ArrayList中删除,然后又添加回该索引,而又不导致ArrayList补偿丢失的项并将空白移到数组列表的末尾,该如何删除呢?

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. 但这会将患者从床上移开后,将bedNumber中的bed移到ArrayList的末尾。 How do I keep it at bedNumber? 如何将它放在bedNumber上?

You want to use a Map object instead. 您想改用Map对象。 I'm a little rusty at java, but you can define a map of beds like this: 我对Java有点生疏,但是您可以定义一张床位图,如下所示:

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. 然后您可以检查输入内容是否为true以查看床是否已被占用,然后在您出院时将其设置为false 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. 您可以将被占床初始化为一定范围的false值,或者可以假设“未设置”状态表示没有人躺在床上。

You just aspecify the index you want - beds.add(bedNumber, patient); 您只需指定所需的索引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. 这推动了另外的位置之前是患者bedNumber后来到bedNumber + 1 ...,从而使您的列表返回到原来的顺序。

public void add(int index, E element) public void add(int index,E元素)

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). 将当前在该位置的元素(如果有)和任何后续元素右移(将其索引添加一个)。

您可以使用beds.get(bedNumber)beds.set(bedNumber, patient)

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. 使用这些解决方案中的任何一个,您的拥有Map或Set的班级可能都有一个成员,该成员指定病房中可用病床的总数。 I wouldn't try to build this concept into the Map such as populating the Map with "null" patients. 我不会尝试将此概念构建到Map中,例如将“ null”患者填充到Map中。

int totalBeds = 10;

if (bedNumber > totalBeds) {
  // send them to the next ward
}

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)". 如果要继续使用ArrayList,则可以使用方法“ set(index,null)”而不是调用方法“ 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 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM