![](/img/trans.png)
[英]How to add an index of an ArrayList to the same Arraylist Java
[英]How to correctly list index values of ArrayList in Java
每次运行这一行代码,索引值都会增加。 第一次运行代码时,索引值是(0,1,2),然后下次在同一测试中运行索引值,则得到相同数组元素的(3,4,5)值。
System.out.println("**********The Book List**********");
for (int i = 0; i < books.size(); i++) {
System.out.print("Index: " + index++);
System.out.print("\t");
System.out.print("Name: " + books.get(i));
System.out.println();
}
有人可以帮我弄清楚我在做什么错吗?
您在循环中使用books.get(i)
,因此看来i
是您要使用的index
。 喜欢,
System.out.print("Index: " + i);
然后删除index
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.