Java 迭代包含ArrayList的ArrayList
我正在处理装袋问题。我得到了一个物品(INT)列表,以及最大尺寸的行李。我正试图找到尽可能少的袋子来装所有的物品。我需要用递归的方法来实现这一点 我正在存储bag对象(即整数的ArrayList,使用一些辅助方法检查size+int是否大于bag的maxSize)。然后我将所有这些包存储在ArrayList对象中。我遇到的问题是,我似乎要把每一件东西都加到每一个包里。下面是我认为问题所在的方法。我想不出一个退出的方法,一旦我发现我可以在“I”处将物品添加到包中。在这一点上,我需要得到一个新的项目,并尝试在第一袋袋,如果它不适合然后尝试第二,第三等 baggedList=ArrayList:: recList=我试图获取的INT列表 我觉得这件事太复杂了。如果你有更好的建议,请告诉我Java 迭代包含ArrayList的ArrayList,java,recursion,arraylist,Java,Recursion,Arraylist,我正在处理装袋问题。我得到了一个物品(INT)列表,以及最大尺寸的行李。我正试图找到尽可能少的袋子来装所有的物品。我需要用递归的方法来实现这一点 我正在存储bag对象(即整数的ArrayList,使用一些辅助方法检查size+int是否大于bag的maxSize)。然后我将所有这些包存储在ArrayList对象中。我遇到的问题是,我似乎要把每一件东西都加到每一个包里。下面是我认为问题所在的方法。我想不出一个退出的方法,一旦我发现我可以在“I”处将物品添加到包中。在这一点上,我需要得到一个新的项目
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;
}
公共布尔打包(ArrayList recList){
布尔值keepBagging=true;
System.out.println(“在递归方法之前”);
System.out.println(“while循环之前的重新列表”+reclist.toString());
而(!recList.isEmpty()&&keepBagging){
int item=recList.remove(recList.size()-1);
对于(int i=0;i
调用包方法的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;
}
private boolean tryToBag(int项,int i){
布尔canBag=false;
Bag currentBag=baggedList.get(i);
ArrayList=currentBag.getList();
if(当前袋、袋(项目)){
canBag=正确;
}
返回罐头袋;
}
罐头法
public boolean canBag(int size){
if (size + Bag.sum() <= maxBagSize){
return true;
}
else return false;
}
public boolean canBag(整数大小){
if(size+Bag.sum()public boolean bagging(ArrayList recList){
布尔值keepBagging=true;
System.out.println(“在递归方法之前”);
System.out.println(“while循环之前的重新列表”+reclist.toString());
而(!recList.isEmpty()&&keepBagging){
int item=recList.remove(recList.size()-1);
对于(int i=0;i
我只是在你的代码中添加了一个中断调用,很难理解你想要的是什么。你不应该重复这个。如果你可以尝试打包并添加一个项目,然后中断并删除下一个项目并循环更多。这是你想要的吗?还是成功后,它应该继续到下一个迭代?em在reclist中,尝试在行李中找到一个插槽,如果发现,则断开for循环并移动到reclist中的下一个项目
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;
}
}
while(!recList.isEmpty()){
int item=recList.remove(recList.size()-1);
对于(int i=0;i
我放弃了这个版本,用一种不同的方法开始了一个新版本。我打算-拿一件物品,试着把它放在第一个袋子里,如果我不能,试着放第二个。如果我不能把它放在第二个袋子里,试着放第三个,依此类推。我最终想把所有的物品放在最少的袋子里。我试着在你休息的时候把它放进去,我得到了第二个e相同的结果。结果是列表是[3,2,1]。maxBag大小是5。输出是Bag0=[3,2],Bag1=[3,2]。这有帮助吗?你能告诉这个baggedList的声明..哪个.size()你在检查循环..!还有addItem()做什么..添加什么?ArrayList baggedList.是声明。'Bag'只是一个ArrayList.大小()是我可以使用的“Bag”数量。在使用此方法之前,我已经初始化了它。我得到了可用的Bag数量。AddItem()通过索引从baggedList中取出“Bag”,然后将其添加到该“Bag”ArrayList。引用您的问题“我正在将所有这些Bag存储在ArrayList对象中。”你不能将这些包对象存储在一个树映射中,maxsize是一个键。因此,maxsize为1的包可以容纳小于或等于1的值。大小为2的包可以容纳小于或等于2的值。我的意思是"我将所有这些包存储在一个包对象的ArrayList中。包对象只是一个ArrayList。我认为这不会起作用。一个包的maxSize可以是X。这反过来可以存储1到多个项目,只要所有这些项目的总和是最少的包数量=项目列表计数/包的最大大小。这是一个递归fu需要操作?这仍然会给出与我得到的结果相同的结果。结果列表为[3,2,1]。maxBag大小为5。输出为Bag0=[3,2],Bag1=[3,2]您可以复制您的tryToBag()方法吗?这可能会有所帮助!我已将它们复制到中。我还复制了我的toString。
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;
}
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;
}
}