创建正确的循环结构来填充对象列表
Creating a correct loop structure to populate a list of objects
我有一个对象列表,每个对象都有两个字段,名称和重量。我需要从那些列表中填充和计算权重,这些列表将满足其他对象列表中对象的参数,但是它必须仅在权重总和不超过最大值时创建这个新对象,然后它继续,从第一个列表计算另一个权重直到最大值,然后为另一个列表创建一个具有所需参数的对象,这就是它在代码中的样子。
这是第一个列表:
[Item{name='building tools', weight=2000}, Item{name='building tools', weight=2000}, Item{name='building tools', weight=2000}, Item{name='building tools', weight=5000}, Item{name='building tools', weight=5000}, Item{name='building tools', weight=2000}, Item{name='building tools', weight=1000}, Item{name='building tools', weight=5000}, Item{name='building tools', weight=6000}, Item{name='shelter equipment', weight=5000}, Item{name='construction equipment', weight=5000}, Item{name='plants', weight=1000}, Item{name='steel', weight=8000}, Item{name='books', weight=1000}, Item{name='water', weight=5000}]
现在我正在创建一个循环,它将权重放入单独的变量中,直到它小于 8000,然后我需要填充其他对象列表,其中一个参数是这些项目的权重,它不能更大超过 8000,这是它在代码中的样子:
public List<Rocket> loadU1(List<Item> items) {
List<Rocket> u1Rockets = new ArrayList<>();
for (int i = 0; i < items.size(); i++) {
if ( testWeight + items.get(i).getWeight() <= 8000) {
testWeight += items.get(i).getWeight();
} else {
u1Rockets.add(new U1(120, 10000, 18000, testWeight));
testWeight = 0;
}
}
return u1Rockets;
}
testWeight 是class中的计算变量,它是一个全局整型变量。新 u1Rocket 的构造函数除了最后一个参数是不能超过 8000 的权重外,所有内容都是静态的。当我 运行 加载 U1 时,它看起来是这样填充的:
[U1{cost=120, weight=10000, maxWeight=18000, weightOfCargo=6000}, U1{cost=120, weight=10000, maxWeight=18000, weightOfCargo=8000}, U1{cost=120, weight=10000, maxWeight=18000, weightOfCargo=6000}, U1{cost=120, weight=10000, maxWeight=18000, weightOfCargo=6000}]
它工作得很好,直到 i=3,然后它当然将 testWeight 设置为 0,因此它可以重新计算它,但不会像应该的那样创建一个新的 5000 火箭对象,因为 5000 之后的那个 ( i = 4) 也是 5000,超过了限制,所以它应该为这个创建单独的 Rocket 5000 然后当然继续......我正在尝试调试它但无法从我这边找出确切的逻辑问题在哪里, 谢谢
所以这是我的火箭 class,
public class Rocket implements Spaceship {
private int price;
private int weight;
private int weightOfCrago;
private int maxWeight;
public Rocket(int price, int weight, int weightOfCrago, int maxWeight) {
this.price = price;
this.weight = weight;
this.weightOfCrago = weightOfCrago;
this.maxWeight = maxWeight;
}
public Rocket() {
}
@Override
public boolean launch() {
return true;
}
@Override
public boolean land() {
return true;
}
@Override
public boolean canCarry(Item item) {
if ((item.getWeight() + weightOfCrago) <= (maxWeight - weight)){
return true;
}else {
return false;
}
}
@Override
public void carry(Item item) {
maxWeight += item.getWeight();
}
例如,不能以某种方式在我之前发布的循环中创建之前使用 canCarry 方法来防止创建超过限制的新 Rocket 对象吗? U1 是火箭的子代。
问题是当火箭已经装满时,您的代码只是简单地跳过了当前选中的项目。
解决这个问题的最快方法是在创建火箭时return到跳过的位置(因此i--
)。但是你也总是跳过最后一个桶,所以你仍然必须添加它。参见:
for (int i = 0; i < items.size(); i++) {
if (testWeight + items.get(i).getWeight() <= 8000) {
testWeight += items.get(i).getWeight();
} else {
u1Rockets.add(new U1(120, 10000, 18000, testWeight));
testWeight = 0;
// re-check the skipped item
i--;
}
}
// add the last rocket
u1Rockets.add(new U1(120, 10000, 18000, testWeight));
请注意,您手上有一个 bin packing problem 的变体,一个 NP 完全问题。
我有一个对象列表,每个对象都有两个字段,名称和重量。我需要从那些列表中填充和计算权重,这些列表将满足其他对象列表中对象的参数,但是它必须仅在权重总和不超过最大值时创建这个新对象,然后它继续,从第一个列表计算另一个权重直到最大值,然后为另一个列表创建一个具有所需参数的对象,这就是它在代码中的样子。
这是第一个列表:
[Item{name='building tools', weight=2000}, Item{name='building tools', weight=2000}, Item{name='building tools', weight=2000}, Item{name='building tools', weight=5000}, Item{name='building tools', weight=5000}, Item{name='building tools', weight=2000}, Item{name='building tools', weight=1000}, Item{name='building tools', weight=5000}, Item{name='building tools', weight=6000}, Item{name='shelter equipment', weight=5000}, Item{name='construction equipment', weight=5000}, Item{name='plants', weight=1000}, Item{name='steel', weight=8000}, Item{name='books', weight=1000}, Item{name='water', weight=5000}]
现在我正在创建一个循环,它将权重放入单独的变量中,直到它小于 8000,然后我需要填充其他对象列表,其中一个参数是这些项目的权重,它不能更大超过 8000,这是它在代码中的样子:
public List<Rocket> loadU1(List<Item> items) {
List<Rocket> u1Rockets = new ArrayList<>();
for (int i = 0; i < items.size(); i++) {
if ( testWeight + items.get(i).getWeight() <= 8000) {
testWeight += items.get(i).getWeight();
} else {
u1Rockets.add(new U1(120, 10000, 18000, testWeight));
testWeight = 0;
}
}
return u1Rockets;
}
testWeight 是class中的计算变量,它是一个全局整型变量。新 u1Rocket 的构造函数除了最后一个参数是不能超过 8000 的权重外,所有内容都是静态的。当我 运行 加载 U1 时,它看起来是这样填充的:
[U1{cost=120, weight=10000, maxWeight=18000, weightOfCargo=6000}, U1{cost=120, weight=10000, maxWeight=18000, weightOfCargo=8000}, U1{cost=120, weight=10000, maxWeight=18000, weightOfCargo=6000}, U1{cost=120, weight=10000, maxWeight=18000, weightOfCargo=6000}]
它工作得很好,直到 i=3,然后它当然将 testWeight 设置为 0,因此它可以重新计算它,但不会像应该的那样创建一个新的 5000 火箭对象,因为 5000 之后的那个 ( i = 4) 也是 5000,超过了限制,所以它应该为这个创建单独的 Rocket 5000 然后当然继续......我正在尝试调试它但无法从我这边找出确切的逻辑问题在哪里, 谢谢
所以这是我的火箭 class,
public class Rocket implements Spaceship {
private int price;
private int weight;
private int weightOfCrago;
private int maxWeight;
public Rocket(int price, int weight, int weightOfCrago, int maxWeight) {
this.price = price;
this.weight = weight;
this.weightOfCrago = weightOfCrago;
this.maxWeight = maxWeight;
}
public Rocket() {
}
@Override
public boolean launch() {
return true;
}
@Override
public boolean land() {
return true;
}
@Override
public boolean canCarry(Item item) {
if ((item.getWeight() + weightOfCrago) <= (maxWeight - weight)){
return true;
}else {
return false;
}
}
@Override
public void carry(Item item) {
maxWeight += item.getWeight();
}
例如,不能以某种方式在我之前发布的循环中创建之前使用 canCarry 方法来防止创建超过限制的新 Rocket 对象吗? U1 是火箭的子代。
问题是当火箭已经装满时,您的代码只是简单地跳过了当前选中的项目。
解决这个问题的最快方法是在创建火箭时return到跳过的位置(因此i--
)。但是你也总是跳过最后一个桶,所以你仍然必须添加它。参见:
for (int i = 0; i < items.size(); i++) {
if (testWeight + items.get(i).getWeight() <= 8000) {
testWeight += items.get(i).getWeight();
} else {
u1Rockets.add(new U1(120, 10000, 18000, testWeight));
testWeight = 0;
// re-check the skipped item
i--;
}
}
// add the last rocket
u1Rockets.add(new U1(120, 10000, 18000, testWeight));
请注意,您手上有一个 bin packing problem 的变体,一个 NP 完全问题。