[英]IndexOutOfBoundsException when adding to ArrayList using nested while loops
I'm trying to create a program which reads a reads a file containing the amount of people in each school district in a state and outputs the total number of people, total number of children, and total number of children below the poverty line.我正在尝试创建一个程序,该程序读取一个文件,该文件包含一个州每个学区的人数,并输出总人数、儿童总数和贫困线以下儿童的总数。 There are 56 states but I can only get the output for 55.
有 56 个状态,但我只能得到 55 个的输出。
I also tried putting int i = 0
inside the first while loop but I just got a bunch of 0s.我也尝试将
int i = 0
放在第一个 while 循环中,但我得到了一堆 0。
int i = 0; int j = 1;
while (j <= Integer.parseInt( popInfo.get(popInfo.size() - 1).getStateCode() )) {
int tPopCt = 0, cPopCt = 0, cPovPopCt = 0;
while (popInfo.get(i).getStateCode().equals(String.format("%02d", j))) {
tPopCt += Integer.parseInt(popInfo.get(i).getTotalPopulation());
cPopCt += Integer.parseInt(popInfo.get(i).getChildPopulation());
cPovPopCt += Integer.parseInt(popInfo.get(i).getChildPovertyPopulation());
i++;
}
output.add(new Output(String.format("%02d", j), tPopCt, cPopCt, cPovPopCt));
j++;
}
I expected the last line of the output to start with 56 (the last StateCode) however I get an error.我希望输出的最后一行以 56(最后一个 StateCode)开头,但是出现错误。 When I set the first while loop to
j < ...
instead of j <= ...
, I get 55 5956920 963445 157356
which is correct but does not include 56.当我将第一个 while 循环设置为
j < ...
而不是j <= ...
,我得到55 5956920 963445 157356
,这是正确的,但不包括 56。
Please help.请帮忙。 TY
泰
The error message and stack trace...错误消息和堆栈跟踪...
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 13486 out of bounds for length 13486
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:458)
at Poverty.<init>(Poverty.java:35)
at Poverty.main(Poverty.java:70)
You should consider redesigning your program.您应该考虑重新设计您的程序。 You didn't wrote what is the type of elements in
popInfo
collection, so let's call it PopData
.你没有写
popInfo
集合中元素的类型,所以我们称之为PopData
。 Maybe you should use one for-each loop instead of nested while loops.也许您应该使用一个 for-each 循环而不是嵌套的 while 循环。 In this case structure of your program could look like (extract values from
data
variable which represents one record of data in your collection):在你的程序的这种情况下,结构可能看起来像(从提取值
data
代表您的集合中的数据的一个记录变量):
int globalTPopCt = 0;
int globalCPopCt = 0;
int globalCPovPopCt = 0;
for(PopData data : popInfo) {
int state = Integer.parseInt(data.getStateCode());
//local counters
int localTPopCt = Integer.parseInt(data.getTotalPopulation());
int localCPopCt = Integer.parseInt(data.getChildPopulation());
int localCPovPopCt = Integer.parseInt(data.getChildPovertyPopulation());
//global (cumulated) counters
globalTPopCt += localTPopCt;
globalCPopCt += localCPopCt;
globalCPovPopCt += localCPovPopCt;
output.add(new Output( ... ));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.