Note that there are some explanatory texts on larger screens.

plurals
  1. POIterate over an ArrayList containing ArrayList
    primarykey
    data
    text
    <p>I am working on a bagging problem. I am given a list of items(ints), and bags that have a maxSize. I am trying to find the least possible amount of bags to fit all the items. I need to do this in a recursive method. </p> <p>I am storing bag objects (That are ArrayList of integers, with some helper methods of checking if the size + an int will be greater than the maxSize of the bag). I am then storing all of these bags in an ArrayList objects. The problem I am running into is I seem to be adding every item to every bag. Below is the method I believe the problem is. I can't think of a way to exit once I have found I can add the item to the bag at 'i'. At this point I need to get a new item, and try to bag it at the first bag, if it doesn't fit then try the second, third etc. </p> <p>baggedList = ArrayList :::: recList = List of ints I am trying to bag </p> <p>I feel like I am over complicating this. If you have any better suggestions please do tell. </p> <pre><code> public boolean bagging (ArrayList&lt;Integer&gt; recList){ boolean keepBagging = true; System.out.println("right before the recursive method"); System.out.println("the reclist before while loop" + recList.toString()); while (!recList.isEmpty() &amp;&amp; keepBagging){ int item = recList.remove(recList.size() -1); for (int i= 0; i &lt; baggedList.size(); i ++){ System.out.println("i = " + i + " Item = " + item); if (tryToBag(item, i)){ //boolean method addItem (item, i); //adds the item bagging (recList); //recursive call } else { System.out.println("FAILED" + keepBagging); keepBagging = false; } } } return keepBagging; } </code></pre> <p>CanBag method that calls a Bag method</p> <pre><code> private boolean tryToBag(int item, int i) { boolean canBag = false; Bag currentBag = baggedList.get(i); ArrayList&lt;Integer&gt; list = currentBag.getList(); if (currentBag.canBag(item)){ canBag = true; } return canBag; } </code></pre> <p>canBag method </p> <pre><code> public boolean canBag(int size){ if (size + Bag.sum() &lt;= maxBagSize){ return true; } else return false; } </code></pre> <p>toString</p> <pre><code> public String toString(){ int counter = 0; String ret = ""; for (int i = 0; i &lt; baggedList.size(); i++){ Bag tryThisBag = baggedList.get(i); ret += "bag " + counter + tryThisBag.getList().toString(); counter++; } return ret; } </code></pre> <p>addItem method</p> <pre><code> private void addItem(int item, int i) { Bag currentBag = baggedList.remove(i); //removing list ArrayList&lt;Integer&gt; list = currentBag.getList(); // new list list.add(item); //adding item System.out.println ("List here at bag" + i + " : " + list.toString()); currentBag.setList(list); // sets the arrayList back to the bag object baggedList.add(i, currentBag); //sets the bag at the index in the collection of bags } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload