I want to filter an array by calculating the differences. Here’s the code:
public void compressData(List<Long> array){
for(int i = 0; i <= array.size(); i++){
if(Math.abs(array.get(i) - (array.get(i + 1))) >= 100){
newArray.add(array.get(i));
}
}
}
Let’s say I have an array of [1, 3, 5, 10]. I want the range between the numbers in array to be at least 5, so in this case the number 3 should be removed. So I want to compare array[i] with array[i + 1]
The code above doesn’t work (I know it won’t work actually, I think I’m just desperate) because array.get(i - 1) or array.get(i + 1) will give ArrayOutOfIndexException
Thank you in advance =)
how about change your
forline to :then you could use array.get(i+1)