I created 4 types of Pod classes:
我创建了4种类型的Pod类:
- Kitchen Pod
- Hygiene Pod
- Sleeping Pod
- Social Pod
Each Pod has a limit of people they can take (Kitchen limit = 8, hygiene limit = 2, sleeping limit = 2, social limit = 10). When the limit is reached, I have to create another Pod of the same type to store the remainder. I have an ArrayList
that stores Pod
objects in a different class called SpaceStationTester
. I am trying to set a specific capacity (number of people) to only the first of each type of Pod
(by calling a capacity setter method that is in each Pod class) without messing with the others (assuming I have more than one pod of "Kitchen", "Sleeping", etc..)
每个Pod都有他们可以接受的限制(厨房限制= 8,卫生限制= 2,睡眠限制= 2,社会限制= 10)。当达到限制时,我必须创建另一个相同类型的Pod来存储剩余部分。我有一个ArrayList,它将Pod对象存储在另一个名为SpaceStationTester的类中。我试图将特定容量(人数)设置为仅每种类型的Pod中的第一个(通过调用每个Pod类中的容量设置器方法)而不会弄乱其他类型(假设我有多个容器“厨房”,“睡觉”等。)
But instead, I end up setting the new capacity for all of the Kitchen, Sleeping, Social and Hygiene pods.
但相反,我最终设置了所有厨房,睡眠,社交和卫生舱的新容量。
How can I only set the capacity of the first of each kind? Here is my SpaceStation class:
我怎样才能设置各种容量?这是我的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);
}
}
Here is my output:
这是我的输出:
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 个解决方案
#1
1
I think it happens because you don't have several of each type; you only have one of each type. You probably assign the same reference to each element in the List. When you change that single instance all the List elements point to the same thing, so it appears that you've "messed them all up".
我认为这是因为你没有几种类型;你只有每种类型中的一种。您可能会为List中的每个元素分配相同的引用。当您更改该单个实例时,所有List元素都指向同一个元素,因此您似乎“将它们全部搞砸了”。
The solution is to create a new Pod type for each element in the List. That way you can change one without affecting the others.
解决方案是为List中的每个元素创建一个新的Pod类型。这样你可以改变一个而不影响其他人。
Here's an example to show what I mean:
这是一个显示我的意思的例子:
import java.util.ArrayList;
import java.util.List;
/**
* Created by Michael
* Creation date 3/9/2016.
* @link https://*.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); }
}
As you can see, I changed one instance of Foo
in my List
; it had no effect on the state of the other.
如您所见,我在列表中更改了一个Foo实例;它对另一个人的状态没有影响。
#2
1
I think you should put flag for each kind to check if the capacity is set. For example:
我认为您应该为每种类型添加标志以检查容量是否已设置。例如:
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;
}
}
#3
0
why dont you just create a separate ArrayList
for each kind of Pod and use it on your iterator
instead of setting the capacity there?
为什么不为每种Pod创建一个单独的ArrayList并在迭代器上使用它而不是在那里设置容量?
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);
}
}
Now that you have them separated, you can load the corresponding pod. (creating a method for each may be good idea)
现在您将它们分开,您可以加载相应的pod。 (为每个创建方法可能是个好主意)
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
When you have them all loaded up, you can add them all up into a single List in case you need to merge them.
当您将它们全部加载完毕后,您可以将它们全部添加到单个列表中,以备需要合并它们时使用。
Not sure if this is what you need, but i hope this helps.
不确定这是否是你需要的,但我希望这会有所帮助。
#1
1
I think it happens because you don't have several of each type; you only have one of each type. You probably assign the same reference to each element in the List. When you change that single instance all the List elements point to the same thing, so it appears that you've "messed them all up".
我认为这是因为你没有几种类型;你只有每种类型中的一种。您可能会为List中的每个元素分配相同的引用。当您更改该单个实例时,所有List元素都指向同一个元素,因此您似乎“将它们全部搞砸了”。
The solution is to create a new Pod type for each element in the List. That way you can change one without affecting the others.
解决方案是为List中的每个元素创建一个新的Pod类型。这样你可以改变一个而不影响其他人。
Here's an example to show what I mean:
这是一个显示我的意思的例子:
import java.util.ArrayList;
import java.util.List;
/**
* Created by Michael
* Creation date 3/9/2016.
* @link https://*.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); }
}
As you can see, I changed one instance of Foo
in my List
; it had no effect on the state of the other.
如您所见,我在列表中更改了一个Foo实例;它对另一个人的状态没有影响。
#2
1
I think you should put flag for each kind to check if the capacity is set. For example:
我认为您应该为每种类型添加标志以检查容量是否已设置。例如:
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;
}
}
#3
0
why dont you just create a separate ArrayList
for each kind of Pod and use it on your iterator
instead of setting the capacity there?
为什么不为每种Pod创建一个单独的ArrayList并在迭代器上使用它而不是在那里设置容量?
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);
}
}
Now that you have them separated, you can load the corresponding pod. (creating a method for each may be good idea)
现在您将它们分开,您可以加载相应的pod。 (为每个创建方法可能是个好主意)
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
When you have them all loaded up, you can add them all up into a single List in case you need to merge them.
当您将它们全部加载完毕后,您可以将它们全部添加到单个列表中,以备需要合并它们时使用。
Not sure if this is what you need, but i hope this helps.
不确定这是否是你需要的,但我希望这会有所帮助。