[英]Out of bounds exception printing in for loop less than arraylist size in java
even though I clearly specify in the for loop that I want it to print until arraylist.size, the terminal returns an OutOfBounds Exception 即使我在for循环中明确指定要打印到arraylist.size为止,终端也会返回OutOfBounds异常
public static void PrintPlayList() {
int lengthsongs = songs.size();
int lengthadds = adds.size();
int amountadds = 0;
for (int i = 0; i < lengthsongs; i++) {
playlist.add(songs.get(i));
if (amountadds < lengthadds) {
playlist.add(adds.get(amountadds));
amountadds++;
}
}
int lengthplaylist = playlist.size();
for (int i = 0; i < lengthplaylist; i++) {
System.out.println(songs.get(i).toString());
System.out.println(adds.get(i).toString());
}
}
And this is what the compiler returns: How is it possible? 这就是编译器返回的结果:怎么可能? How can I fix it? 我该如何解决?
java.lang.IndexOutOfBoundsException: Index: 18, Size: 18
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Main.PrintPlayList(Main.java:153)
at Main.Interface(Main.java:55)
at Main.main(Main.java:34)
If you use a debugger, you should see that the playlist
has more elements than either songs
or adds
as it is the sum of these (though not quite) This error is produced at runtime not compile time. 如果使用调试器,则应该看到playlist
中的元素比songs
或adds
元素多,因为它们是这些元素的总和(尽管不完全相同)。此错误是在运行时未编译时产生的。
The simplest solution is 最简单的解决方案是
public static void printPlayList() {
playlist.clear();
playlist.addAll(songs);
playlist.addAll(adds);
playlist.forEach(System.out::println);
}
or just 要不就
public static void printPlayList() {
songs.forEach(System.out::println);
adds.forEach(System.out::println);
}
I think the problem is here: 我认为问题出在这里:
for (int i = 0; i < lengthplaylist; i++) {
System.out.println(songs.get(i).toString());
System.out.println(adds.get(i).toString());
}
lengthplaylist
can be larger then lengthsongs
in your code. lengthplaylist
可以大于代码中的lengthsongs
。 And you're using for loop to go from 0 to lengthplaylist
instead of lengthsongs
. 而且您正在使用for循环从0到lengthplaylist
而不是lengthsongs
。
You should also give attention to adds.get(i).toString()
because for this to execute all the lengths must be same. 您还应该注意adds.get(i).toString()
因为要执行此操作,所有长度必须相同。
You may just use different variables in your loop for them like this: 您可以像这样在循环中使用不同的变量:
for (int i = 0,j=0; i < lengthsongs && j<lengthadds; i++,j++) {
System.out.println(songs.get(i).toString());
System.out.println(adds.get(j).toString());
}
The list playlist
is not initialized inside PrintPlayList()
, so it might contain elements before. 列表playlist
未在PrintPlayList()
内初始化,因此它可能之前包含元素。
In this loop: 在此循环中:
for (int i = 0; i < lengthsongs; i++) {
playlist.add(songs.get(i));
if (amountadds < lengthadds) {
playlist.add(adds.get(amountadds));
amountadds++;
}
}
you add all elements of songs
to playlist
, so playlist
has at least as many elements as songs
, but does it contain more? 您将songs
所有元素添加到playlist
,因此playlist
至少具有与songs
一样多的元素,但是它包含更多元素吗? I don't know but you can find out by debugging. 我不知道,但是您可以通过调试找出答案。
The same applies to the list adds
. 同样适用于列表adds
。
Do they all have the same size? 它们大小都一样吗? This loop: 这个循环:
for (int i = 0; i < lengthplaylist; i++) {
System.out.println(songs.get(i).toString());
System.out.println(adds.get(i).toString());
}
will succeed if they do have the same size or playlist
's size is less than the other 2 lists. 如果它们的大小相同或playlist
的大小小于其他两个列表,则将成功。
ArrayList in Java is indexed from 0. You are accessing index 18, however your list is contains elements from 0 to 17. Java中的ArrayList的索引从0开始。您正在访问索引18,但是您的列表包含从0到17的元素。
Since you store your songs
and adds
in playlists
, you can print them directly from the playlist
like this: 由于您存储了songs
并adds
了playlists
,因此您可以像这样直接从playlist
打印它们:
for (int i = 0; i < lengthplaylist; i++) {
System.out.println(playlist.get(i).toString());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.