如何获取以前的 ArrayList 值?
How to get previous ArrayList value?
这是项目的数组列表:
项目 1:(重量:4,profit:5)
第 2 项:(权重:10,profit:12)
第 3 项:(权重:5,profit: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
How to get previous ArrayList value after .set() is used?
除非您使用 new ArrayList<Integer>(myListToBackup)
拥有列表的备份副本,否则您不能
这是项目的数组列表:
项目 1:(重量:4,profit:5)
第 2 项:(权重:10,profit:12)
第 3 项:(权重:5,profit: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
How to get previous ArrayList value after .set() is used?
除非您使用 new ArrayList<Integer>(myListToBackup)