线程"main"中的异常java.lang.IndexOutOfBoundsException:索引:10,大小:0.JAVA [英] Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 0. JAVA
本文介绍了线程"main"中的异常java.lang.IndexOutOfBoundsException:索引:10,大小:0.JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 ArrayList
表示优先级队列.所以我想在 ArrayList
的特定位置添加项目.但是当我运行它时,系统告诉我线程"main"中的异常java.lang.IndexOutOfBoundsException:索引:10,大小:0.
I want to use an ArrayList
to represent the priority queue. So I want to add item on specific position of the ArrayList
. But when I run it, system told me Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 0.
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();
}}
推荐答案
查看全文