I am currently working on a program that replaces the value in an array if the value next to it is the same as the current value. So If the array is [0,0,0,1,0,1,0], when the program runs it'll turn to [0,0,1,1,1,1,1]. Currently, the code works to an extent, it runs but I still get an “Index out of bounds exception” after it terminates, and it prints out one less value than it should. So an array that had 5 elements ends up having 4 elements. Here's my code:
int arr[] = {0,1,0,0,1,0,0};
int days = 0;
int arr[] = {0,1,0,0,1,0,0};
do {
int n = arr.length;
for(int i=0; i<arr.length; i++) {//iterates through the array and subs in 0s for duplicates and 1s for regulars
if(arr[i] == arr[i+1]) {
arr[i] = 0;
}else{
arr[i] = 1;
}
System.out.println(" "+arr[i]);
}
days++;
}while(days<=30);
What can I do to fix that issue and stop the error after the program runs?