如何在不干扰其他对象的情况下操作arrayList中的一个对象?

时间:2016-03-10 03:51:23

标签: java object arraylist

我创建了4种Pod类:

  1. Kitchen Pod
  2. Hygiene Pod
  3. 睡觉荚
  4. Social Pod
  5. 每个Pod都有他们可以接受的限制(厨房限制= 8,卫生限制= 2,睡眠限制= 2,社会限制= 10)。当达到限制时,我必须创建另一个相同类型的Pod来存储剩余部分。我有ArrayListPod个对象存储在名为SpaceStationTester的其他类中。我试图将特定容量(人数)设置为每种Pod中的第一种(通过调用每个Pod类中的容量设置器方法)而不会弄乱其他类型(假设我有更多比一个“厨房”,“睡觉”等的豆荚......)

    但相反,我最终设置了所有厨房,睡眠,社交和卫生容器的新容量。

    我怎样才能设置各种容量?这是我的SpaceStation课程:

    public class SpaceStationTester {
        public static void main(String[] args) {
            /* Step 1: Create space station that will support 15 colonists.
                       Report the configuration of all pods created. 
            */
            SpaceStation station = new SpaceStation(15);
            String configuration = station.getStationConfig();
            System.out.println(configuration);
    
    
            /* Step 2:  Assign colonists to pods as follows:
    
                5 are in social pods
                8 are in kitchen pods
                1 is in a hygiene pod
                1 is in a sleeping pod.
    
              Check the status of all pods and report it.
            */
            int socialAmount = 5;
            int kitchenAmount = 8;
            int hygieneAmount = 1;
            int sleepingAmount = 1;
    
            ArrayList<Pod> arr = new ArrayList<Pod>();
            arr.addAll(station.getPods()); //Adding everything from pods to arr
            Iterator<Pod> it = arr.iterator();
    
            while (it.hasNext()) {
                Pod x = it.next();
                if (x.getType() == "Kitchen Pod") {
                    x.setCurrentCapacity(kitchenAmount);
                } else if (x.getType() == "Sleeping Pod") {
                    x.setCurrentCapacity(sleepingAmount);
                } else if (x.getType() == "Hygiene Pod") {
                    x.setCurrentCapacity(hygieneAmount);
                } else if (x.getType() == "Social Pod") {
                    x.setCurrentCapacity(socialAmount);
                }
            }
    
            String status = station.checkStatus();
            System.out.println(status);
        }
    }
    

    这是我的输出:

    Pod type:Kitchen Pod    Current capacity:8  Max capacity:8
    Pod type:Kitchen Pod    Current capacity:8  Max capacity:8
    Pod type:Sleeping Pod   Current capacity:1  Max capacity:2
    Pod type:Sleeping Pod   Current capacity:1  Max capacity:2
    Pod type:Sleeping Pod   Current capacity:1  Max capacity:2
    Pod type:Sleeping Pod   Current capacity:1  Max capacity:2
    Pod type:Sleeping Pod   Current capacity:1  Max capacity:2
    Pod type:Sleeping Pod   Current capacity:1  Max capacity:2
    Pod type:Sleeping Pod   Current capacity:1  Max capacity:2
    Pod type:Sleeping Pod   Current capacity:1  Max capacity:2
    Pod type:Hygiene Pod    Current capacity:1  Max capacity:2
    Pod type:Hygiene Pod    Current capacity:1  Max capacity:2
    Pod type:Hygiene Pod    Current capacity:1  Max capacity:2
    Pod type:Hygiene Pod    Current capacity:1  Max capacity:2
    Pod type:Hygiene Pod    Current capacity:1  Max capacity:2
    Pod type:Hygiene Pod    Current capacity:1  Max capacity:2
    Pod type:Hygiene Pod    Current capacity:1  Max capacity:2
    Pod type:Hygiene Pod    Current capacity:1  Max capacity:2
    Pod type:Social Pod     Current capacity:5  Max capacity:10
    Pod type:Social Pod     Current capacity:5  Max capacity:10
    

3 个答案:

答案 0 :(得分:1)

我认为这是因为你没有几种类型;你只有每种类型中的一种。您可能会为List中的每个元素分配相同的引用。当您更改该单个实例时,所有List元素都指向同一个元素,因此您似乎已经将它们全部搞砸了#34;。

解决方案是为List中的每个元素创建一个新的Pod类型。这样你可以改变一个而不影响其他人。

这是一个展示我的意思的例子:

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Michael
 * Creation date 3/9/2016.
 * @link https://stackoverflow.com/questions/35907239/how-can-i-manipulate-one-object-in-the-arraylist-without-messing-with-the-other/35907280#35907280
 */
public class Foo {
    private int value;

    public static void main(String [] args) {
        List<Foo> foos = new ArrayList<Foo>();
        foos.add(new Foo(1));
        foos.add(new Foo(2));

        System.out.println("before change: " + foos);
        foos.get(0).setValue(20);
        System.out.println("after  change: " + foos);
    }

    public Foo(int v) { this.setValue(v); }

    public void setValue(int v) { this.value = v; }

    public String toString() { return String.format("value: %d", this.value); }
}

如您所见,我在Foo中更改了List的一个实例;它对另一方的状态没有影响。

答案 1 :(得分:1)

我认为您应该为每种类型添加标记以检查容量是否已设置。 例如:

    boolean isKitchenPodSet = false, isSleepingPodSet = false,
    isHygienePodSet = false, isSocialPodSet = false;
    while (it.hasNext()) {
        Pod x = it.next();
        if (x.getType() == "Kitchen Pod" && !isKitchenPodSet ) {
            x.setCurrentCapacity(kitchenAmount);
            isKitchenPodSet = true;    
        } else if (x.getType() == "Sleeping Pod" && !isSleepingPodSet ) {
            x.setCurrentCapacity(sleepingAmount);
            isSleepingPodSet = true;
        } else if (x.getType() == "Hygiene Pod" && !isHygienePodSet ) {
            x.setCurrentCapacity(hygieneAmount);
            isHygienePodSet = true;
        } else if (x.getType() == "Social Pod" && !isSocialPodSet ) {
            x.setCurrentCapacity(socialAmount);
            isSocialPodSet  = true;
        }
    }

答案 2 :(得分:0)

为什么不为每种Pod创建单独的ArrayList并在iterator上使用它而不是在那里设置容量?

List<Pod> kitchenPods = new ArrayList<Pod>();
// ... do the same for the remaining kinds of pod

while (it.hasNext()) {
        Pod x = it.next();
        if (x.getType() == "Kitchen Pod") {
            kitchenPods.add(x);
        } else if (x.getType() == "Sleeping Pod") {
            sleepingPods.add(x);
        } else if (x.getType() == "Hygiene Pod") {
            hygienePods.add(x);
        } else if (x.getType() == "Social Pod") {
           socialPods.add(x);
        }
    }

现在您已将它们分开,您可以加载相应的窗格。 (为每个方法创建一个方法可能是个好主意)

    for(int i=0; i < kitchenPods.size(); i++){
         if (kitchenPods.get(i).getCapacity < kitchenPods.get(i).getMaxCapacity){
                 //  Load them up!
          }
    }

    // do the same for the rest of the Pods

当你把它们全部加载完毕后,你可以将它们全部添加到一个List中,以备你需要合并它们。

不确定这是否是您所需要的,但我希望这会有所帮助。