如何获取以前的ArrayList值?

时间:2016-06-28 15:53:39

标签: java arraylist bit

这是项目的arraylist:

第1项:(重量:4,利润:5)
第2项:(重量:10,利润:12)
第3项:(重量:5,利润:8)

和capacity = 11以及要翻转的随机位(如果0将变为1,反之亦然):
orderList = [2,0,1]。

我的代码:

'

public class BitString {
   public static void main (String[] args){
    int n = 3, capacity = 11, pointer, toFlip;

    ArrayList<Item> itemList = new ArrayList<Item>();
    ArrayList<Integer> solution = new ArrayList<Integer>();
    ArrayList<Integer> currentSolution = new ArrayList<Integer>();
    ArrayList<Integer> flipOrder = new ArrayList<Integer>();
    ArrayList<ArrayList<Integer>> improve = new   ArrayList<ArrayList<Integer>>();

    itemList.add(new Item(4,5));
    itemList.add(new Item(10,12));
    itemList.add(new Item(5,8));

    solution = initialSolution(n);
    currentSolution = solution;
    flipOrder = randomFlipOrder(n);

    System.out.println("List of Items: " + itemList);
    System.out.println("Initial solution: " + solution);
    System.out.println("Current solution: " + currentSolution);
    System.out.println("Random order: " + flipOrder);

    for (int i = 0; i < flipOrder.size(); i++){
        int totalWeight = 0, totalProfit = 0;

        pointer = flipOrder.get(i);
        toFlip = solution.get(pointer);

        System.out.println();

        for (int j = 0; j < solution.size(); j++){
            if (solution.get(j) == 1){
                totalWeight += itemList.get(j).getWeight();
                totalProfit += itemList.get(j).getProfit();
            }
        }

        System.out.println("Total Weight For Solution " + solution + " : " + totalWeight + " | Total Profit For Solution " + solution + " : " + totalProfit);

        if (totalWeight <= capacity){
            System.out.println(totalWeight + " NOT EXCEED CAPACITY FOR SOLUTION: " + solution);
            currentSolution = solution;
            improve.add(currentSolution);
            System.out.println("Updated Current Solution: " + solution);
            System.out.println("Updated Improved: " + improve);

            //do the flipping bits
            if (toFlip == 1)
                solution.set(pointer, 0);
            else
                solution.set(pointer, 1);

            System.out.println("New Solution After flip: " + solution);
            //improve.remove(0);
        }
        else{
             System.out.println(totalWeight + " EXCEEDS CAPACITY FOR SOLUTION: " + solution);
            //solution = currentSolution;
            System.out.println("SOLUTION REVERTED: " + improve.get(0));

            //do the flipping bits

            if (toFlip == 1)
                 solution.set(pointer, 0);
            else
                solution.set(pointer, 1);

            System.out.println("New Solution After flip: " + solution);
        }

    }

}

//generate initial solution(bits) randomly
public static ArrayList<Integer> initialSolution(int length){
     Random r = new Random();
    ArrayList<Integer> solution = new ArrayList<Integer>(length);

    // generate some random boolean values
    boolean[] booleans = new boolean[length];
    for (int i = 0; i < booleans.length; i++) {
      booleans[i] = r.nextBoolean();
    }

    for (boolean b : booleans) {
      if (b == true){
          solution.add(1);
      }
      else{
          solution.add(0);
       }
    }

    return solution;

}

public static ArrayList<Integer> randomFlipOrder(int length){
     ArrayList<Integer> order = new ArrayList<Integer>();
     Random r = new Random();

     for (int i = 0; i < length; i++){
        order.add(i);
    }

    Collections.shuffle(order);

    return order;
   }
  }

&#39;

生成随机位串 例如: [0,1,0] 表示采用第2项,总重量= 10。

因此,如果总重量&lt; =容量,则将 [0,1,0] 保留在arraylist。

然后,我需要翻转索引2处的位(基于orderList):
例如: [0,1,1] 表示第2项和第2项。取3并且总重量= 15。

我想收回以前存储的值 [0,1,0] ,并使用之前的值工作:
[0,1,0] =&gt;下一个要翻转的位是在索引0处并且变为 [1,1,0] 而不是采用最新的(超出容量)[0,1,1]并翻转它[1,1,1] ]

但是我不断获得更新的值,并且无论何时超出容量都无法获得之前的值。

我的输出: Output Image

1 个答案:

答案 0 :(得分:0)

  

如何在使用.set()之后获取以前的ArrayList值?

除非您使用new ArrayList<Integer>(myListToBackup)

获得列表的备份副本,否则无法使用