Reputation: 23
I ArrayLists, the data includes months of the year 2015, 2016 and 2017.
Data is grouped as - For Year 2015, months are from 05 to 12 - For 2016, months are from 01 to 12 and - For 2017, month 01 to 06 - Months are in sequence as:
[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]
What I am trying to do is: counting the number of months is repeated in the consecutive cell in the array. For example 05 month is repeated 3 times and month 6 is repeated 25 times and so on. But it gives me the following error:
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)
And here is my Approach
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 size is 534 in my case
Hope I have provided enough information
Thanks in advance
Upvotes: 0
Views: 91
Reputation: 369
(int mn = 0; mn <= monitoringMonthArray.size(); mn++)
this will iterate size + 1 times.
(int mn = 0; mn < monitoringMonthArray.size(); mn++)
iterates size times,
but since you are accessing mn +1
th item , you should loop for size -1
times.
(int mn = 0; mn < monitoringMonthArray.size()-1; mn++)
this should work.
Upvotes: 0
Reputation: 1289
Here is the solution if I get your question right:
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;
}
And 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]
[]
Upvotes: 0
Reputation: 6946
In your for loop check mn <= monitoringMonthArray.size()
you are have situation where mn
is equal to size of your list. List (and arrays) in java are indexed from 0
to size - 1
.
So, if you have list with size==1
, calling get(1)
is trying to get second element.
Another thing is that monitoringMonthArray.get(mn + 1)
can exceed your size either.
You condition should be:
for(int mn = 0; mn < monitoringMonthArray.size() - 1; mn++)
Upvotes: 2
Reputation: 131356
This condition makes no sense :
for(int mn = 0; mn <= monitoringMonthArray.size(); mn++){
as monitoringMonthArray
is an ArrayList
and get(int)
with as parameter the size of the List
is out of the bound of the ArrayList
.
So as in the loop you write :
monitoringMonthArray.get(mn)
you will necessarily finish with a IndexOutOfBoundsException
that is thrown.
Besides, this condition in the loop body:
if(monitoringMonthArray.get(mn).equals(monitoringMonthArray.get(mn + 1))){
requires you iterate at most until the last-1 element for the same reason :
So the loop condition should be :
for(int mn = 0; mn < monitoringMonthArray.size()-1; mn++){
Upvotes: 0