Let's say I have an array:
int[] array = new int[10];
What is the execution time:
int len = array.length;
I would have thought it would be a constant operation of the time, but today in an interview the interviewer told me that it would be O(n)because the number of elements would need to be calculated.
Also, if I have a loop like this:
for (int i = array.length - 1; i >=0; i--) {
something with array[i];
}
Is it due to additional operations nto get to the end of the array in order to start the loop? The interviewer came from the C-background, so maybe they were wrong about how Java works, but I didn’t want to push it during the interview.
source
share