在java中,如何调用数组列表中的特定元素?

在java中,如何调用数组列表中的特定元素?,java,arraylist,Java,Arraylist,如果我有一个数组列表,比如 ArrayList <Student> list = new ArrayList <Student>(); Student s1 = new Student("Alex S", "Smashing smile", 12); Student s2 = new Student("James L", "Beautiful voice", 12); Student s3 = new Student("Alex C", "Star VB player

如果我有一个数组列表,比如

ArrayList <Student> list = new ArrayList <Student>();
Student s1 = new Student("Alex S",  "Smashing smile", 12);
Student s2 = new Student("James L", "Beautiful voice", 12);
Student s3 = new Student("Alex C",  "Star VB player", 12);
如何调用索引为2的元素作为Student的每个特定实例的编号?

ArrayList具有public E getint index函数:该函数返回此列表中指定位置的元素

list.add(s1);
list.add(s2);
list.add(s3);
Student student = list.get(2);
查看文档中的其他函数。

当您从ArrayList中创建对象时,您会得到。。对象本身

假设您有一个返回此数字的方法,您可以:

list.get(2).getNumber(); //Where getNumber() returns this 12 you want..

您可以使用getint index方法。请注意,ArrayList的第一个元素的索引为0。

调用X.get2,其中X={instance1,instance2,instance3,…}。我现在知道了一种方法,可以一次调用所有实例。

对于每个特定实例?你是指列表吗;方法?list.get2?您应该查看以下文档:。如果您想拥有所有数量的学生实例,那么应该实现for循环。对于学生s:列出System.out.printlns.getNumber;