[英]Java method decrementing values but not reassigning
好吧,我的主课有两个课,分别是Process
和一个称为Scheduler
的课。
目的:基本上创建一系列流程以及剩余时间。 调用调度类,该类实际上从timeRemaining
删除了数字1
这是主要方法:
public static void main(String[] args) {
// TODO code application logic here
Random rn = new Random();
Scheduler scheduler = new Scheduler();
for(int i = 1; i<=5; i++){
double rand = rn.nextInt(10);
Process process = new Process(i,rand);
scheduler.addObj(process); // adds the object to the array
}
scheduler.printQueue(scheduler.getList());
System.out.println("");
scheduler.sortQueue(scheduler.getList());
scheduler.printQueue(scheduler.getList());
for(int i =0; i <10; i++){
System.out.println("");
scheduler.scheduleNext(scheduler.getList());
scheduler.printQueue(scheduler.getList());
System.out.println("");
}
}
这将创建5个存储在对象数组中的进程。 然后,我按剩余时间的降序对数组进行排序。 这工作得很好。
问题是从0到10的循环。我要使之执行的是调用方法scheduleNext(),该方法选择使用哪个数组,然后选择哪个值发送schedule方法。
这是scheduleNext()方法:
public void scheduleNext(ArrayList<Process> list){
Process firstElement = list.get(0);
if (firstElement.schedule(firstElement.getTimeRemaining()) == true){
list.remove(0);
}
else{
Collections.rotate(list,-1);
}
}
这是调度方法:
public boolean schedule(double timeRemaining){
if(timeRemaining < 1){
this.timeRemaining = 0;
return true;
}
else{
this.timeRemaining = timeRemaining -1;
return false;
}
}
我已经调试了很多次,它减少了timeRemaining并将其移到数组的底部。 但是,当我打印出来时,它只是打印出正常值...我是否将它们正确分配了?
这是完整的代码 (流程类)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package process;
import java.util.Random;
/**
*
* @author Luke
*/
public class Process implements Comparable<Process> {
/**
* @param args the command line arguments
*/
private int processId;
private double timeRequired;
private double timeRemaining;
public Process(int processId, double timeRequired) {
this.processId = processId;
this.timeRequired = timeRequired;
this.timeRemaining = timeRequired;
}
public double getTimeRemaining() {
return timeRemaining;
}
public static void main(String[] args) {
// TODO code application logic here
Random rn = new Random();
Scheduler scheduler = new Scheduler();
for(int i = 1; i<=5; i++){
double rand = rn.nextInt(10);
Process process = new Process(i,rand);
scheduler.addObj(process);
}
scheduler.printQueue(scheduler.getList());
System.out.println("");
scheduler.sortQueue(scheduler.getList());
scheduler.printQueue(scheduler.getList());
for(int i =0; i <10; i++){
System.out.println("");
scheduler.scheduleNext(scheduler.getList());
scheduler.printQueue(scheduler.getList());
System.out.println("");
}
}
public void setProcessId(int processId) {
this.processId = processId;
}
public void setTimeRequired(double timeRequired) {
this.timeRequired = timeRequired;
}
public int getProcessId() {
return processId;
}
public double getTimeRequired() {
return timeRequired;
}
@Override
public int compareTo(Process o) {
if (this.timeRequired < o.timeRequired){
return -1;
}else if (this.timeRequired > o.timeRequired){
return 1;
}else{
return 0;
}
}
public boolean schedule(double timeRemaining){
if(timeRemaining < 1){
this.timeRemaining = 0;
return true;
}
else{
this.timeRemaining = timeRemaining -1;
return false;
}
}
@Override
public String toString() {
return "Process{" + "processId=" + processId + ", timeRequired=" + timeRequired + '}';
}
}
( 计划程序类 )
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package process;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
* @author Luke
*/
public class Scheduler {
ArrayList<Process> list = new ArrayList<>();
public ArrayList<Process> getList() {
return list;
}
public void addObj(Process p){
list.add(p);
}
public void sortQueue(ArrayList<Process> list){
Collections.sort(list);
}
public void printQueue(ArrayList<Process> list){
for(Process i: list){
System.out.println(i);
}
}
public void scheduleNext(ArrayList<Process> list){
Process firstElement = list.get(0);
if (firstElement.schedule(firstElement.getTimeRemaining()) == true){
list.remove(0);
}
else{
Collections.rotate(list,-1);
}
}
}
好吧,代码工作正常。 您必须将timeRequired
误打印为timeRemaining
,将其减少。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.