以下是項目的ArrayList:如何獲取先前的ArrayList值?
項目1:(重量:4,利潤:5)
第2項:(重量:10,利潤:12)
第3項:(重量:5,利潤:8)
和容量= 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;
}
}
'
具有生成隨機比特串。
例如:[0,1,0]意味着項目2取和給予總重量= 10。
因此,如果總重量< =容量然後保持[0,1,0]在數組列表。
然後,我需要翻轉索引2的比特(基於orderList):
例如:[0,1,1]意味着項目2 & 3取出並給予總重量= 15。
我想收回存儲[0,1,0]和從先前的值工作的先前值:
[0,1,0] =>下一個比特是倒裝在索引0處,並bcomes [1,1,0]而不是採取最新(超容量)[0,1,1]並翻轉它[1,1,1]。
但是我不斷收到更新的值,並且無論何時超過容量都無法獲取先前的值。
我的輸出: Output Image
你真的應該問的問題列表*的問題*的身體備份副本,而不是僅僅在標題...並請減少你的問題到[mcve]。目前還不清楚你要問什麼,但如果你想知道舊值,在調用set()前調用'get()'... –