我是ArrayLists,数据包括2015年,2016年和2017年的几个月。
数据分组为 - 2015年,月份为05至12 - 2016年,月份从01到12 - 2017年,01至06月 - 月份依次为:
[05, 05, 05, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 07, 07, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 06, 07, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 08, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 09, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 03, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 04, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05, 05]
我想做的是: 在阵列的连续单元格中重复计算月数。 例如,05个月重复3次,第6个月重复25次,依此类推。但它给了我以下错误:
java.lang.IndexOutOfBoundsException: Index: 534, Size: 534
Execution Completed
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at yearandmonth.task(yearandmonth.java:84)
这是我的方法
int counter = 1;
ArrayList<Integer> monthCounterArray = new ArrayList<Integer>();
for(int mn = 0; mn <= monitoringMonthArray.size(); mn++){
if(monitoringMonthArray.get(mn).equals(monitoringMonthArray.get(mn + 1))){
counter++;
}
else {
System.out.println("Month " + monitoringMonthArray.get(mn)+ " Repeated " + counter);
monthCounterArray.add(counter);
counter = 1;
}
}
在我的案例中,BTW monitoningMonthArray大小为534
希望我提供了足够的信息
提前致谢
答案 0 :(得分:2)
在for循环检查mn <= monitoringMonthArray.size()
中,您的情况mn
等于列表的大小。 java中的List(和数组)从0
索引到size - 1
。
因此,如果您有size==1
的列表,则调用get(1)
会尝试获取第二个元素。
另一件事是monitoringMonthArray.get(mn + 1)
也可能超出你的规模。
你的条件应该是:
for(int mn = 0; mn < monitoringMonthArray.size() - 1; mn++)
答案 1 :(得分:0)
这种情况没有意义:
for(int mn = 0; mn <= monitoringMonthArray.size(); mn++){
monitoringMonthArray
为ArrayList
,get(int)
为参数,List
的大小超出ArrayList
的范围。
所以你在循环中写道:
monitoringMonthArray.get(mn)
你必须以抛出的IndexOutOfBoundsException
完成。
此外,循环体中的这个条件:
if(monitoringMonthArray.get(mn).equals(monitoringMonthArray.get(mn + 1))){
要求你最多迭代,直到最后一个元素出于同样的原因:
所以循环条件应该是:
for(int mn = 0; mn < monitoringMonthArray.size()-1; mn++){
答案 2 :(得分:0)
create()
这将迭代大小+ 1次。
(int mn = 0; mn <= monitoringMonthArray.size(); mn++)
迭代大小时间,
但是由于您正在访问(int mn = 0; mn < monitoringMonthArray.size(); mn++)
项,因此您应该循环mn +1
次。
size -1
这应该有效。
答案 3 :(得分:0)
如果我的问题是正确的,这是解决方案:
public static void main(String[] args) {
List<String> monthCounterArray = Arrays.asList(new String[] { "05", "05", "05", "06", "06", "06", "06", "06", "06", "06", "06",
... <your array>
"05", "05" });
List<String> monthCounterArray_1 = Arrays.asList(new String[]{ "05", "05", "05", "06", "06", "06", "07", "07", "08", "09", "09", "10", "10",
"11", "12", "12", "12", "01", "01", "01", "01", "01", "02", "03", "04", "04", "04", "04", "04", "05",
"05", "05", "05", "06", "07", "08", "09", "10", "11", "12", "01", "01", "01", "01", "01"});
List<String> monthCounterArray_2 = Arrays.asList(new String[] {});
List<Integer> output = countOccurences(monthCounterArray);
List<Integer> output_1 = countOccurences(monthCounterArray_1);
List<Integer> output_2 = countOccurences(monthCounterArray_2);
System.out.println(Arrays.toString(output.toArray()));
System.out.println(Arrays.toString(output_1.toArray()));
System.out.println(Arrays.toString(output_2.toArray()));
}
private static List<Integer> countOccurences(List<String> input) {
List<Integer> output = new ArrayList<>();
String previousValue = "-1";
int k = 0;
for (String fromInput : input) {
if (previousValue.equals("-1")) {
output.add(k, 1);
previousValue = fromInput;
} else if (previousValue.equalsIgnoreCase(fromInput)) {
output.set(k, output.get(k) + 1);
} else {
if (k != 0) {
output.set(k, output.get(k) + 1);
}
previousValue = "-1";
k++;
}
}
return output;
}
输出:
[3, 25, 2, 15, 25, 25, 25, 25, 21, 21, 25, 25, 25, 15, 26, 25, 25, 25, 25, 21, 21, 25, 25, 24]
[3, 3, 2, 3, 2, 4, 5, 2, 5, 4, 2, 2, 2, 5]
[]