[英]my integer counter doesn't work if i apply more than 1 method
我使用的是supplyCounter,它是一個全局變量,用於計算制造商為庫存生成新隨機數的次數。
public class test {
private static Random manufacturer;
private static ArrayList<Integer> ordersForFirstFit, stocksForFirstFit, ordersForBestFit, stocksForBestFit;
private static int supplyCounter = 0;
public test() {
}
public static void main(String[] args)
{
ordersForFirstFit = createCustomerOrders(1000);
ordersForBestFit = ordersForFirstFit;// using same data as first fit for best fit
stocksForFirstFit = createStockOfRope();
stocksForBestFit = stocksForFirstFit;// using same data as first fit for best fit
manufacturer = new Random();
supplyCounter = 0;
firstFitAlgorithm(stocksForFirstFit, ordersForFirstFit);
//showCustomerOrders(ordersForFirstFit);//for testing and debugging
//showStockOfRope(stocksForFirstFit);//for testing and debugging
supplyCounter = 0;
bestFitAlgorithm(stocksForBestFit, ordersForBestFit);
//showCustomerOrders(ordersForBestFit);//for testing and debugging
//showStockOfRope(stocksForBestFit);//for testing and debugging
}
public static ArrayList<Integer> createCustomerOrders(int n) {
ArrayList<Integer> ropeOrder = new ArrayList<Integer>();
Random randomOrders = new Random();
for(int i = 0; i < n+1; i++)
{
ropeOrder.add(randomOrders.nextInt(100)+1);// because the range include 0 but exclude 100, therefore +1
//System.out.println("Rope Orders "+i+": "+ropeOrder.get(i));
}
return ropeOrder;
}
public static void showCustomerOrders(ArrayList<Integer> ropeOrder) {
for(int i = 0; i < ropeOrder.size()-1; i++)
{
System.out.println("Rope Orders "+i+": "+ropeOrder.get(i));
}
}
public static ArrayList<Integer> createStockOfRope() {
ArrayList<Integer> ropeStock = new ArrayList<Integer>();
Random randomStocks = new Random();
for(int i = 0; i < 6; i++)
{
ropeStock.add(i,randomStocks.nextInt(100)+101);//for a range between 100 to 200
//System.out.println("Rope(s) in Stock "+i+": "+ropeStock.get(i));
}
return ropeStock;
}
public static void showStockOfRope(ArrayList<Integer> ropeStock) {
for(int i = 0; i < ropeStock.size()-1; i++)
{
System.out.println("Rope(s) in Stock "+i+": "+ropeStock.get(i));
}
}
public static void insertionSort(ArrayList<Integer> data) {
//initialize variables
int key = 0, j = 0;
for(int i = 1; i < data.size(); i++)
{
key = data.get(i);
j = i;
while( (j>0)&&(key < data.get(j-1)) )
{
data.set(j, data.get(j-1));
j = j-1;
}
data.set(j, key);
}
}
public static void firstFitAlgorithm(ArrayList<Integer> ropeStock, ArrayList<Integer> ropeOrder) {
//int supplyCounter = 0;
long begin = System.nanoTime();
for(int i = 0; i < ropeOrder.size(); i++)
{
int j = 0;
while( ropeOrder.get(i) > ropeStock.get(j) )
{
if(j == ropeStock.size()-1)
{
ropeStock.add(j,manufacturer.nextInt(100)+101);//Buy a rope from manufacturer then put the rope to the stock
supplyCounter++;
//then check to see whether it fits the requirement, if not, do the previous step again
}
else
j++;
}
if( ropeOrder.get(i) <= ropeStock.get(j) )
{
if(ropeStock.get(j) - ropeOrder.get(i) > 5)//Assume rope coils shorter than 5m are scrapped
{
ropeStock.set(j, ropeStock.get(j) - ropeOrder.get(i));
ropeOrder.set(i, 0);
}
else
{
ropeStock.remove(j);
if(ropeStock.size() == 0)//when the last rope in stock is sold, buy a new coil of rope from manufacturer
ropeStock.add(manufacturer.nextInt(100)+101);
supplyCounter++;
ropeOrder.set(i, 0);
}
}
}
long total = System.nanoTime() - begin;
System.out.println("The time spent for Firstfit is: "+total+" nanoSeconds");
System.out.println("The number of rope supplied by manufacturer for Firstfit is: "+supplyCounter+" ropes");
}
public static void bestFitAlgorithm(ArrayList<Integer> ropeStock, ArrayList<Integer> ropeOrder) {
//int supplyCounter = 0;
long begin = System.nanoTime();
insertionSort(ropeStock);
insertionSort(ropeOrder);
int j = 0;
for(int i = 0; i < ropeOrder.size(); i++)
{
while( ropeOrder.get(i) > ropeStock.get(j) )
{
if(j == ropeStock.size()-1)
{
ropeStock.add(manufacturer.nextInt(100)+101);//Buy a rope from manufacturer then put the rope to the stock
insertionSort(ropeStock);//then check to see whether it fits the requirement, if not, do the previous step again
j++;
supplyCounter++;
}
else
j++;
}
if( ropeOrder.get(i) <= ropeStock.get(j) )
{
if(ropeStock.get(j) - ropeOrder.get(i) > 5)//Assume rope coils shorter than 5m are scrapped
{
ropeStock.set(j, ropeStock.get(j) - ropeOrder.get(i));
insertionSort(ropeStock);
ropeOrder.set(i, 0);
}
else
{
ropeStock.remove(j);
if(ropeStock.size() == 0)//when the last rope in stock is sold, buy a new coil of rope from manufacturer
ropeStock.add(manufacturer.nextInt(100)+101);
supplyCounter++;
ropeOrder.set(i, 0);
}
j = 0;
}
}
long total = System.nanoTime() - begin;
System.out.println("The time spent for Bestfit is: "+total+" nanoSeconds");
System.out.println("The number of rope supplied by manufacturer for Bestfit is: "+supplyCounter+" ropes");
}
}
輸出是
Firstfit花費的時間是:5009697 nanoSeconds
制造商為Firstfit提供的繩索數量是:649條繩索
Bestfit花費的時間是:5358148 nanoSeconds
制造商為Bestfit提供的繩索數量是:0條繩索
但是,如果我只運行bestfit算法,則計數器返回一個不為0的合理值
public static void main(String[] args)
{
ordersForFirstFit = createCustomerOrders(1000);
ordersForBestFit = ordersForFirstFit;// using same data as first fit for best fit
stocksForFirstFit = createStockOfRope();
stocksForBestFit = stocksForFirstFit;// using same data as first fit for best fit
manufacturer = new Random();
supplyCounter = 0;
bestFitAlgorithm(stocksForBestFit, ordersForBestFit);
}
結果:
Bestfit花費的時間是:31895376 nanoSeconds
制造商為Bestfit提供的繩索數量是:465根繩索
我也嘗試在方法內創建計數器而不是使用全局變量,但是得到了相同的結果。
有人知道發生了什么嗎?
您的問題是您正在兩種算法之間共享數據。
更改
ordersForBestFit = ordersForFirstFit;
至
ordersForBestFit = new ArrayList<Integer>(ordersForFirstFit);
並改變
stocksForBestFit = stocksForFirstFit;
至
stocksForBestFit = new ArrayList<Integer>(stocksForFirstFit);
ArrayList的內容將是相同的整數。 但是,當您在第一個算法中對列表進行排序時,它會影響ArrayList的狀態,這會給您的下一個算法帶來問題。 通過創建一個新的ArrayList並將其傳遞給第二個算法,可以避免第一個算法的副作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.