Java方法递减值但不重新分配
好的,我主要有两个类,Java方法递减值但不重新分配,java,arrays,list,Java,Arrays,List,好的,我主要有两个类,Process和一个名为Scheduler 目标:基本上创建一系列流程及其剩余时间。调用schedule类,该类从timeRemaining 以下是主要方法: public static void main(String[] args) { // TODO code application logic here Random rn = new Random(); Scheduler scheduler = new Sche
Process
和一个名为Scheduler
目标:基本上创建一系列流程及其剩余时间。调用schedule类,该类从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); // 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("");
}
}
publicstaticvoidmain(字符串[]args){
//此处的TODO代码应用程序逻辑
Random rn=新的Random();
调度器调度器=新调度器();
对于(int i=1;i来说,代码工作正常。您一定是把打印的timeRequired
错当成了减少的timeRemaining
。您需要检查timeRemaining
的值,所以将其添加到toString
方法中。类似这样的内容:返回“Process{+”processId=”+processId+”,timeRequired=“+timeRequired+”,TimeLeving=“+TimeLeving+”}';
我无法处理.toString(),因为对象仅在forloop中,对吗?
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;
}
}
/*
* 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);
}
}
}