Java 无法在同一类中创建类的ArrayList

Java 无法在同一类中创建类的ArrayList,java,arraylist,Java,Arraylist,问题就在这两方面 Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at TestArray.function(TestArray.java:11) at test.m

问题就在这两方面

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at TestArray.function(TestArray.java:11)
    at test.main(test.java:17)
a1.setQueue(新的ArrayList(1));
test1 test2=a1.getQueue().get(0);
什么

newarraylist(1)
是创建初始容量为1的ArrayList,但不向其中添加任何对象


因此,a1中的队列没有任何元素,这就是为什么您正在获取
IndexOutOfBoundsException

您正在尝试从空列表中获取内容。
test1 test2=a1.getQueue().get(0)
此时,
数组列表中没有任何内容,它是空的
new ArrayList(1)
,此处的数字允许
ArrayList
为内部缓存“保留”一定量的空间,这有助于提高
ArrayList
的性能,它不会添加任何内容。。。
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at TestArray.function(TestArray.java:11)
    at test.main(test.java:17)
a1.setQueue(new ArrayList<test1>(1));
test1 test2=a1.getQueue().get(0);
new ArrayList<test1>(1)