優先キューを表すのにArrayList
を使用します。だから私はArrayList
の特定の位置にアイテムを追加したいと思います。私はそれを実行したときしかし、システムがException in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 0.
スレッド "main"の例外java.lang.IndexOutOfBoundsException:インデックス:10、サイズ:0 JAVA
public class PriorityQueue
{
public ArrayList<String> Queue=new ArrayList<>();
public void enqueu(String s, int p)
{
Queue.add(p,s);
}
public void dequeu()
{
String temp=Queue.get(Queue.size()-1);
Queue.remove(temp);
}
public void print()
{
String[] print=new String[Queue.size()];
print=Queue.toArray(print);
for(int i=0;i<Queue.size();i++)
{
System.out.println(print[i]);
}
}
public static void main(String[] args)
{
PriorityQueue test= new PriorityQueue();
test.enqueu("x",10);
test.enqueu("Y",1);
test.enqueu("Z",3);
test.print();
}}
どのようなデバッグ試行をしましたか? https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –
あなたの 'enqueue' doeは' enqueue'が最後に追加されるので、これが待ち行列になっていれば意味をなさないキューの任意のインデックスではない。 Priority Queueクラスをコールするので、 'enqueue'メソッドの2番目のパラメータは、インデックスではなく要素の優先順位になります。あなたはおそらくQueuesでいくつか読むべきです。 –