简体   繁体   English

遍历包含ArrayList的ArrayList

[英]Iterate over an ArrayList containing ArrayList

I am working on a bagging problem. 我正在处理装袋问题。 I am given a list of items(ints), and bags that have a maxSize. 我得到了具有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. 我需要以递归的方式做到这一点。

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). 我正在存储包对象(它们是整数的ArrayList,带有一些检查大小+整数是否大于包的maxSize的辅助方法)。 I am then storing all of these bags in an ArrayList objects. 然后,我将所有这些包存储在ArrayList对象中。 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'. 一旦发现可以在“ 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. 此时,我需要获取一个新商品,并尝试将其放在第一个袋子中,如果不合适,请尝试第二个,第三个等。

baggedList = ArrayList :::: recList = List of ints I am trying to bag baggedList = ArrayList :::: recList =我要装袋的整数列表

I feel like I am over complicating this. 我觉得我已经太复杂了。 If you have any better suggestions please do tell. 如果您有任何更好的建议,请告诉。

    public boolean bagging (ArrayList<Integer> 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() && keepBagging){
        int item = recList.remove(recList.size() -1);
        for (int i= 0; i < 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;
}

CanBag method that calls a Bag method 调用Bag方法的CanBag方法

    private boolean tryToBag(int item, int i) {

    boolean canBag = false;
    Bag currentBag = baggedList.get(i);
    ArrayList<Integer> list = currentBag.getList();

    if (currentBag.canBag(item)){
        canBag = true;
    }
    return canBag;
}

canBag method canBag方法

    public boolean canBag(int size){

    if (size + Bag.sum() <= maxBagSize){
        return true;
    }
    else return false;
}

toString 的toString

    public String toString(){
    int counter = 0;
    String ret = "";
    for (int i = 0; i < baggedList.size(); i++){
        Bag tryThisBag = baggedList.get(i);
        ret += "bag " + counter + tryThisBag.getList().toString();
        counter++;
    }
    return ret;
}

addItem method addItem方法

    private void addItem(int item, int i) {
    Bag currentBag = baggedList.remove(i); //removing list
    ArrayList<Integer> 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

}
public boolean bagging (ArrayList<Integer> 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() && keepBagging) {
    int item = recList.remove(recList.size() -1);
    for (int i= 0; i < baggedList.size(); i ++){
        System.out.println("i = " + i + " Item = " + item);
        if (tryToBag(item, i)){ //boolean method
            addItem (item, i); //adds the item
            break;
        }
        else {
            System.out.println("FAILED" + keepBagging);
            keepBagging = false;
        }
    }
}
return keepBagging;
}

I merely just added a break call into your code, it's hard to understand what you are going for. 我只是在代码中添加了一个break调用,很难理解您要做什么。 You shouldn't need to recurse with this. 您不需要对此进行递归。 If you can try to bag and you add an item, then break and then remove the next item and loop over some more. 如果您可以尝试装袋并添加一个项目,则先折断再移除下一个项目,然后再遍历其他项目。 Is this what you wanted? 这就是你想要的吗? Or on success, should it continue to the next iteration? 还是成功后,是否应该继续下一次迭代?

For each item in reclist, try to find a slot in bags, if found break out of for loop and move to the next item in the reclist 对于清单中的每个项目,尝试找到一个装在袋子中的插槽,如果找到了,则打破for循环并移至清单中的下一个项目

while (!recList.isEmpty()) {
int item = recList.remove(recList.size() -1);
for (int i= 0; i < baggedList.size(); i ++){
    System.out.println("i = " + i + " Item = " + item);
    if (tryToBag(item, i)){ //boolean method
        addItem (item, i); //adds the item
    }
  break;
  }
}

我取消了此版本,并以不同的方式开始了新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM