Java 跳过数字的First Fit装箱算法

Java 跳过数字的First Fit装箱算法,java,javafx,bin-packing,Java,Javafx,Bin Packing,我正在试着做第一件合适的箱子包装。这是我编写的代码,每行的解释都作为注释: private void runFirstFit(ActionEvent event) { // The counters int i; int j = 0; // The boolean packingComplete = false; // Declare an arrayList from the numbers the user has entered

我正在试着做第一件合适的箱子包装。这是我编写的代码,每行的解释都作为注释:

private void runFirstFit(ActionEvent event) {
    // The counters
    int i;
    int j = 0;

    // The boolean
    packingComplete = false;

    // Declare an arrayList from the numbers the user has entered
    ArrayList<Integer> numbers = new ArrayList(6);

    // Add numbers into the array from input (using a loop)
    for (i = 0; i < 6; i++) {
        numbers.add(parseInt(getNumber(i)));
    }

    // - Main packing algorithm starts here -
    // Iterate through arraylist and get next number
    Iterator<Integer> iterator = numbers.iterator();

    // While there are still numbers left, try and add to bins
    while (iterator.hasNext()) {
        // Number(s) still exist in the list
        // Check if number can fit inside bin
        System.out.println("Number currently in queue: " + iterator.next());

        if (canNumberFitInsideBin(j, iterator.next())) {
            // Put number inside bin
            bin[j] += String.valueOf(iterator.next()) + ", ";

            System.out.println("Number added to bin " + j);
        } else {
            // Bin is full, move to the next bin (increment counter)
            j++;

            // Put number inside that bin
            bin[j] += String.valueOf(iterator.next()) + ", ";

            System.out.println("Counter incremented");
        }
    }

    // Update all labels
    updateAllBinLabels();
}
private void runFirstFit(ActionEvent事件){
//柜台
int i;
int j=0;
//布尔值
打包完成=错误;
//根据用户输入的数字声明arrayList
ArrayList编号=新的ArrayList(6);
//从输入向数组中添加数字(使用循环)
对于(i=0;i<6;i++){
add(parseInt(getNumber(i));
}
//-主打包算法从这里开始-
//遍历arraylist并获取下一个数字
迭代器迭代器=数字。迭代器();
//虽然仍有剩余数量,但请尝试添加到垃圾箱中
while(iterator.hasNext()){
//列表中仍存在个数
//检查号码是否可以放在箱子里
System.out.println(“当前队列中的编号:+iterator.next());
if(cannumberfitinidebin(j,iterator.next()){
//把号码放进箱子里
bin[j]+=String.valueOf(iterator.next())+“,”;
系统输出打印项次(“添加到箱子的编号”+j);
}否则{
//箱子已满,移动到下一个箱子(增量计数器)
j++;
//把号码放进那个箱子里
bin[j]+=String.valueOf(iterator.next())+“,”;
System.out.println(“计数器递增”);
}
}
//更新所有标签
updateAllBinLabels();
}
基本上,
getNumber(i)
部分是一个返回数字的函数。我使用循环将实际数字(更具体地说,是其中的6个)添加到名为“数字”的ArrayList中

我试着在每个阶段打印出数字,看看它处理的是哪个数字——但它似乎只是无缘无故地随机跳过了一些数字。例如,当数组列表输入为
1,2,3,4,5,6
时,它添加到
bin[0]
的第一个数字是
3
(应该是
1
),然后它还会将
6
添加到
bin[0]
,并忽略所有其他数字,然后转到下一个bin数组

谁能看出我做错了什么


谢谢

最明显的问题是迭代器.next()在循环中的每个条目只应调用一次。每次你打电话,你都在名单上前进。您需要调用它一次,并将其保存在循环顶部的临时变量中


此外,您可能还应该检查数字是否可以放入else中的下一个存储箱中,除非您知道没有任何值大于您的存储箱大小。

在程序的初始阶段,它不会允许用户输入大于存储箱大小的数字。我使用了一种不同的方法来存储数字——使用普通数组而不是arraylist,我的代码现在可以工作了。但是,由于某种原因,移动迭代器.next()会使程序停留在第一个数字上。