以下是项目的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()'... –