卡在故障循环中

时间:2018-12-20 10:11:28

标签: java while-loop

我很长时间以来一直在家庭作业中遇到这个问题,希望您的专业帮助。 我需要模拟装满准备用于“火星任务”的火箭,并在阵列列表中排列各种物品。给定每个火箭(包括货物)的最大重量(18,000 Kg),以及火箭的净重(10,000 Kg)和每个项目的重量(项目对象包括“重量”和“ itemType”字段,例如“建筑材料”,“水”等。)

说明要填充每枚火箭,直到其满载为止,然后再制造另一枚。看来我的同学们忽略了这条指令,因此他们的代码无济于事。

根据重量对数组进行升序/降序排序不能解决问题。

我的问题是,尽管我使用了while循环,但是火箭仍然拒绝填充,尽管仍然有剩余空间可以填充列表中剩余的项目。循环不会跳过接下来的2-3个项目(我设法得到的最多是跳过一个项目)并找到仍然可以加载的一个项目。

同伴也是项目列表。

public ArrayList<U1> loadU1(ArrayList<Item> items) {

    ArrayList<U1> fleetU1 = new ArrayList();

    int i = 0;

    Iterator<Item> iterator = items.iterator();
    while (iterator.hasNext()) {

        U1 rocketU1 = new U1(); // create new rocket with zero cargo

         while (rocketU1.canCarry(items.get(i))) { // "canCarry" checks if item's weight fits in:

/* public final boolean canCarry(Item cargo){
        if(currentRocketWeight + cargo.weight <= maxRocketWeight){
            return true;
        } else {
            return false; }} */


 rocketU1.carry(items.get(i));

// "carry" updates rocket total weight - no more than 18000 Kg including rocket net weight 10000 Kg, i.e. max cargo weight is 8000 Kg:

/* public final int carry(Item cargo){
           currentRocketWeight += cargo.weight;
           return currentRocketWeight;}
      */
           items.remove(i); // remove loaded item from list

          }

        fleetU1.add(rocketU1); // add rocket to fleet
    }

    return fleetU1;
}

/*arraylist "items" - "phase-1.txt":

building tools=2000
building tools=2000
building tools=2000
building tools=5000
building tools=5000
building tools=2000
building tools=1000
building tools=5000
building tools=6000
shelter equipment=5000
construction equipment=5000
plants=1000
steel=8000
books=1000
water=5000*/

public ArrayList<Item> loadItems(int phaseNum) {

try {
        switch (phaseNum) {
            case 1:
                out.println("Loading phase 1:");
                fileName = "phase-1.txt";
                break;
            case 2:
                out.println("Loading phase 2:");
                fileName = "phase-2.txt";
                break;
            default:
            out.println("argument must be 1 or 2");
        }

        File file = new File(fileName);
        Scanner scanner = new Scanner(file);

        while (scanner.hasNextLine()) {
            lineFromFile = scanner.nextLine();

            String[] list = lineFromFile.split("=");
            Item item = new Item(); //(list[0], );
            item.itemType = list[0];
            item.weight = Integer.parseInt(list[1]);

            itemList.add(item); // create ArrayList of items
        }

        scanner.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return itemList;

}

3 个答案:

答案 0 :(得分:0)

您需要一种方法来填充火箭,然后在仍有货物要装载时循环调用该方法。为了使这项工作无需递归调用,我将一系列火箭“ fleet”更改为班级成员

private List<Item> loadRocket(List<Item> items) {        
    Iterator<Item> iterator = items.iterator();
    List<Item> loaded = new ArrayList<>();
    U1 rocketU1 = new U1();

    while (iterator.hasNext()) {
        Item item = iterator.next();
        if (rocketU1.canCarry(item)) {
            rocketU1.carry(item);
            loaded.add(item);
        } 
    }
    items.removeAll(loaded);
    fleetU1.add(rocketU1);
    return items;
}

然后循环调用

while (!items.isEmpty()) {
    items = loadRocket(items);
}

如果您不想拥有“舰队as a class member you could move the creation of the rocket and adding to the舰队list to outside of ´loadRocket,而是将其作为参数发送。

while (!items.isEmpty()) {
    U1 rocket = new U1();
    items = loadRocket(rocket, items);
    fleet.add(rocket);
}

答案 1 :(得分:0)

您需要迭代每枚火箭的物品。

// create a copy of the items so we can remove without harm to the caller
List<Items> ourList = new ArrayList<>(items);
// you should probably sort the list by weight, descending order

while (!ourList.isEmpty()) {
    // we need a rocket.
    U1 rocket = new U1();
    // go through all the items and load if item fits
    Iterator<Item> iterator = ourList.iterator();
    while (iterator.hasNext()) {
        // the next() call that's mentioned in the comment
        Item item = iterator.next();
        if (rocket.canCarry(item)) {
            rocket.carry(item);
            // you need to remove from the iterator itself, not from the list
            // or you will get an exception because that makes the iterator invalid
            // it will remove from the underlying list as well though
            iterator.remove();
        }
    }
    fleet.add(rocket);
}

答案 2 :(得分:0)

这就是我要做什么:

对于每个项目,我都会尝试将其放置在舰队中已经存在的火箭中;如果不可能的话,我会向舰队增加一枚新火箭。如果该项目甚至无法放入空火箭中,也将保留在输入列表中,否则将其删除:

public List<U1> loadU1(List<Item> items) {
    List<U1> fleetU1 = new ArrayList<>();
    Iterator<Item> iterator = items.iterator();
    while (iterator.hasNext()) {
        Item item = iterator.next();
        U1 rocketU1 = null;
        for (U1 u1: fleetU1) {
            if (u1.canCarry(item)) {
                rocketU1 = u1;
                break;
            }
        }
        if (rocketU1 == null) {
            rocketU1 = new U1();
            if (!rocketU1.canCarry(item)) {
                // the item is too heavy
                continue;
            }
            fleetU1.add(rocketU1);
        }
        rocketU1.carry(item);
        iterator.remove();
    }
    return fleetU1;
}