我正在創建一些不同的CPU調度算法的模擬,並且也想同時學習一種新的語言。我遇到的麻煩是試圖根據他們的到達時間對進程的數組進行排序。我遇到的錯誤是沒有爲Collections.sort(進程)找到合適的方法,我不確定該怎麼做。在java中排序對象的arraylist
Process.java
import java.util.Random;
public class Process implements Comparable
{
private Random generator;
private String name;
private int burstTime;
private int priority;
private int arrivalTime;
/**
* Process constructor where the user choose the values
* @param nameValue name of the process
* @param burstTimeValue burst time of the process
* @param priorityValue priority of the process
* @param arrivalTimeValue arrival time of the process
*/
public Process(String nameValue, int burstTimeValue, int priorityValue,
int arrivalTimeValue)
{
name = nameValue;
burstTime = burstTimeValue;
priority = priorityValue;
arrivalTime = arrivalTimeValue;
}
/**
* Process constructor that randomizes the values of
* name, burst time, priority, and arrival time.
*/
public Process()
{
generator = new Random();
name = "Process" + generator.nextInt(10000);
burstTime = generator.nextInt(10);
priority = generator.nextInt(5);
arrivalTime = generator.nextInt(30);
}
/**
* Returns the name of the process
* @return name the name of the process
*/
public String getName()
{
return name;
}
/**
* Sets the name of the process
* @param aValue value to set the process name to
*/
public void setName(String aValue)
{
name = aValue;
}
/**
* Returns the burst time of the process
* @return burstTime the burst time of the process
*/
public int getBurstTime()
{
return burstTime;
}
/**
* Sets the burst time of a process
* @param aValue the value for the burst time of a process
*/
public void setBurstTime(int aValue)
{
burstTime = aValue;
}
/**
* Returns the priority value of the process
* @return priority the priority of the process
*/
public int getPriority()
{
return priority;
}
/**
* Sets the priority of a process
* @param aValue value for priority
*/
public void setPriority(int aValue)
{
priority = aValue;
}
/**
* Returns the arrival time of the process
* @return arrival time the arrival time of the process
*/
public int getArrivalTime()
{
return arrivalTime;
}
/**
* Sets the arrival time value
* @param aValue value for arrival time
*/
public void setArrivalTime(int aValue)
{
arrivalTime = aValue;
}
/**
* Overrides the toString method from the String class
* Returns a printout of the object's variables
* @return printout of the object's variables
*/
@Override
public String toString()
{
return "Process[name=" + name + " bTime=" + burstTime
+ " priority=" + priority + " aTime=" + arrivalTime +"]";
}
/**
* Compares two process objects
* @param otherObject another process object
* @return the order of two processes
*/
@Override
public int compareTo(Object otherObject)
{
Process other = (Process) otherObject;
if (arrivalTime < other.arrivalTime) return -1;
if (arrivalTime == other.arrivalTime) return 0;
return 1;
}
}
Comparable.java
public interface Comparable
{
int compareTo(Object otherObject);
}
Cpu.java
import java.util.ArrayList;
import java.util.Collections;
public class Cpu implements
{
private ArrayList<Process> processes;
/**
*
*/
public Cpu()
{
processes = new ArrayList<Process>();
}
/**
* Adds a process to the ArrayList
* @param p the process that is being added
*/
public void addProcess(Process p)
{
processes.add(p);
}
public void sort()
{
Collections.sort(processes);
}
}
你實現我的項目清單(產品模型類) 「Comparable」,並且你有一個「compareTo()」方法 - 很好。但是你不想要「processes.sort()」而不是「Collections.sort(processes)」? – paulsm4
哎呀 - 我沒有注意到你正在寫你自己的*「接口Comparable」。正如Sibbo指出的那樣 - 這不會起作用;) – paulsm4