2011-11-03 99 views
2

我正在创建一些不同的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); 
    }  
} 
+1

你实现我的项目清单(产品模型类) “Comparable”,并且你有一个“compareTo()”方法 - 很好。但是你不想要“processes.sort()”而不是“Collections.sort(processes)”? – paulsm4

+0

哎呀 - 我没有注意到你正在写你自己的*“接口Comparable”。正如Sibbo指出的那样 - 这不会起作用;) – paulsm4

回答

3

您需要实现java.lang.Comparable。编写自己的代码不会起作用,因为它在另一个包中。

因此,只需在您的Process类中添加导入声明并删除您自制的Comparable接口。 (+查看第一条评论)

+4

这个人+1。此外,我建议实施“可比较的”而不是“可比较”。这样你就不必投掷你的东西。 –

0

我做了一个函数,按照模型类中的特定变量对对象列表进行排序。它可能不会直接匹配你需要的,但也许你可以接受这个想法。我使用反射来排序任何数据类型的类模型。

public Vector sort(Vector list, String kelas, String s, String asc) throws NoSuchMethodException { 

     try { 
      // Creates an object of type Class which contains the information of 
      // the class String 
      Object[] obj = list.toArray(); 
      Object[] args = {}; 
      Class cl = Class.forName(kelas); 
      Method toSort = cl.getMethod(s, null); 
      if (asc.equalsIgnoreCase("desc")) { 
       if (toSort.getReturnType().toString().equalsIgnoreCase("int") || toSort.getReturnType().toString().equalsIgnoreCase("double")) { 
        for (int i = 0; i < obj.length; i++) { 
         for (int j = i; j < obj.length; j++) { 
          try { 
           if (Double.parseDouble(toSort.invoke(obj[i], args).toString()) < Double.parseDouble(toSort.invoke(obj[j], args).toString())) { 
            Object temp = obj[i]; 
            obj[i] = obj[j]; 
            obj[j] = temp; 
           } 
          } catch (IllegalAccessException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (IllegalArgumentException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (InvocationTargetException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } 
         } 
        } 
       } else { 
        for (int i = 0; i < obj.length; i++) { 
         for (int j = i; j < obj.length; j++) { 
          try { 
           if (toSort.invoke(obj[i], args).toString().compareToIgnoreCase(toSort.invoke(obj[j], args).toString()) < 0) { 
            Object temp = obj[i]; 
            obj[i] = obj[j]; 
            obj[j] = temp; 
           } 
          } catch (IllegalAccessException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (IllegalArgumentException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (InvocationTargetException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } 
         } 
        } 
       } 
      } else { 
       if (toSort.getReturnType().toString().equalsIgnoreCase("int") || toSort.getReturnType().toString().equalsIgnoreCase("double")) { 
        for (int i = 0; i < obj.length; i++) { 
         for (int j = i; j < obj.length; j++) { 
          try { 
           if (Double.parseDouble(toSort.invoke(obj[i], args).toString()) > Double.parseDouble(toSort.invoke(obj[j], args).toString())) { 
            Object temp = obj[i]; 
            obj[i] = obj[j]; 
            obj[j] = temp; 
           } 
          } catch (IllegalAccessException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (IllegalArgumentException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (InvocationTargetException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } 
         } 
        } 
       } else { 
        for (int i = 0; i < obj.length; i++) { 
         for (int j = i; j < obj.length; j++) { 
          try { 
           if (toSort.invoke(obj[i], args).toString().compareToIgnoreCase(toSort.invoke(obj[j], args).toString()) > 0) { 
            Object temp = obj[i]; 
            obj[i] = obj[j]; 
            obj[j] = temp; 
           } 
          } catch (IllegalAccessException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (IllegalArgumentException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (InvocationTargetException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } 
         } 
        } 
       } 
      } 

      list = new Vector(); 
      for (int i = 0; i < obj.length; i++) { 
       list.add(obj[i]); 
      } 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 
     return list; 
    } 

你可以调用该函数像这样简单:

Vector sortedList=sort(UnsortedList, "bean.Items", "getItemName", "asc"); 

这条线将排序基于项目名称升