【发布时间】:2013-12-22 00:22:17
【问题描述】:
// ArrayList
import java.io.*;
import java.util.*;
public class ArrayListProgram
{
public static void main (String [] args)
{
Integer obj1 = new Integer (97);
String obj2 = "Lama";
CD obj3 = new CD("BlahBlah", "Justin Bieber", 25.0, 13);
ArrayList objects = new ArrayList();
objects.add(obj1);
objects.add(obj2);
objects.add(obj3);
System.out.println("Contents of ArrayList: "+objects);
System.out.println("Size of ArrayList: "+objects.size());
BodySystems bodyobj1 = new BodySystems("endocrine");
BodySystems bodyobj2 = new BodySystems("integumentary");
BodySystems bodyobj3 = new BodySystems("cardiovascular");
objects.add(1, bodyobj1);
objects.add(3, bodyobj2);
objects.add(5, bodyobj3);
System.out.println();
System.out.println();
int i;
for(i=0; i<objects.size(); i++);
{
System.out.println(objects.get(i));
}
} }
for 循环正在尝试使用 size() 方法打印数组列表的内容。如何停止收到 ArrayIndexOutOfBounds 错误?
我的数组列表中有索引 0-5(6 个对象)。
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at ArrayListProgram.main(ArrayListProgram.java:37)
【问题讨论】:
-
请在以后花时间格式化您的代码,使其更具可读性,并提供简短但完整的程序来演示问题。
标签: java arrays loops for-loop arraylist