2011-11-03 9 views
2

私はいくつかの異なるCPUスケジューリングアルゴリズムのシミュレーションを作成していると同時に、新しい言語を学びたいと思っていました。私がやっている問題は、到着時間に基づいてプロセスのarraylistを分類しようとしていることです。私が実行しているエラーは、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()"メソッドを持っています - 良い。しかし、 "Collections.sort(processes)"の代わりに "processes.sort()"が必要なのですか? – paulsm4

+0

おっとり - あなた自身の* "インターフェイスComparable"を書いていたことに気付かなかった。 Sibboが指摘したように - それはうまくいかない;) – paulsm4

答えて

3

あなたはjava.lang.Comparableを実装する必要があります。他のパッケージに入っているので、自分で書くことはできません。

したがって、Processクラスのインポート宣言を追加して、独自のComparableインターフェイスを削除してください。

+4

+1この男。また、単に 'Comparable'ではなく' Comparable 'を実装することをお勧めします。あなたのオブジェクトを投げる必要はありません。 –

2

到着時間に基づいて2つのProcessオブジェクトを比較するカスタムComperatorを作成することができます。 Collectionsクラスには、コレクション(リスト、配列)とComperatorをとるオーバーロードされたsort()メソッドがあります。ここで

がそうするための優れた例です。

http://www.javadeveloper.co.in/java-example/java-comparator-example.html

同様Sibboの応答を参照してください。

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"); 

この行は、あなたが実装しようとしている

関連する問題