Greedy Algorithm Java / firstFit method
<p>I am taking a data structures and algorithms with Java class at my local community college, and I am completely stuck on my current homework assignment. The problem is as follows...</p>

<p>Write a program that packs the objects of various weights into containers. Each container can hold a max of 10 pounds. </p>

<p>The program uses a greedy algorithm that places an object into the first bin in which it will fit. </p>

<p>I am not asking for my homework to be done for me, I am just really hoping to be pointed in the right direction. I have the program really close to working but I just can't get it to function 100% properly. I am able to get the first container to hold the right amount of weight, but then after that the rest of my containers only hold one weight value per container. Here is what I have so far....</p>

<pre><code>import java.util.ArrayList;


public class Lab20 {
    public static void main(String[] args) {
        final java.util.Scanner input = new java.util.Scanner(System.in);

    System.out.print("Enter the number of objects: ");
    double[] items = new double[input.nextInt()];
    System.out.print("Enter the weight of the objects: ");
    for (int i = 0; i &lt; items.length; i++) {
        items[i] = input.nextDouble();
    }

    ArrayList&lt;Bin&gt; containers = firstFit(items);

    //Display results
    for (int i = 0; i &lt; containers.size(); i++) {
        System.out.println("Container " + (i + 1)
                + " contains objects with weight " + containers.get(i));
    }
    input.close();
}

//Greedy Algorithm??
public static ArrayList&lt;Bin&gt; firstFit(double[] items) {
    ArrayList&lt;Bin&gt; list = new ArrayList&lt;&gt;();
    Bin bin = new Bin();

    list.add(bin);

    for (int i = 0; i &lt; items.length; i++) {
        if (!bin.addItem(items[i])) {
            Bin bin2 = new Bin();
            list.add(bin2);
            bin2.addItem(items[i]);
            }
        }
        return list;
    }
}

//Bin Class
class Bin {
    private ArrayList&lt;Double&gt; objects = new ArrayList&lt;&gt;();
    private double maxWeight = 10;
    private double totalWeight = 0;

    public Bin() {
    }

    public Bin(double maxWeight) {
        this.maxWeight = maxWeight;
    }

    //Or is this supposed to be the Greedy algorithm??
    public boolean addItem(double weight) {
        if ((totalWeight+weight) &lt;= maxWeight) {
            objects.add(weight);
            totalWeight += weight;
            return true;
        }
        else {
            return false;
        }
    }

    public int getNumberOfObjects() {
        return objects.size();
    }

    @Override
    public String toString() {
        return objects.toString();
    }
}
</code></pre>

<p>And here is the output that I am getting...</p>

<p>Enter the number of objects: 6</p>

<p>Enter the weight of the objects: 7 5 2 3 5 8</p>

<p>Container 1 contains objects with weight [7.0, 2.0]</p>

<p>Container 2 contains objects with weight [5.0]</p>

<p>Container 3 contains objects with weight [3.0]</p>

<p>Container 4 contains objects with weight [5.0]</p>

<p>Container 5 contains objects with weight [8.0]</p>

<hr>

<p>And this is what the output should be...</p>

<p>Enter the number of objects: 6</p>

<p>Enter the weight of the objects: 7 5 2 3 5 8</p>

<p>Container 1 contains objects with weight [7.0, 2.0]</p>

<p>Container 2 contains objects with weight [5.0, 3.0]</p>

<p>Container 3 contains objects with weight [5.0]</p>

<p>Container 4 contains objects with weight [8.0]</p>