Suppose we have an array myarray of length n, and we say:

int n = myarray.length;

What is the Big O of this statement?

  • A: O(1)
  • B: O(n)
  • C: O(n log n)
  • D: O(n2)
  • E: depends on the item type of myarray

    Answer: A

    An Array is a reference type, which contains its .length as a stored value. We can tell that .length is a stored value rather than a method because it has no parentheses after it.

    Rule: any stored .field of a reference type can be accessed in O(1) time.

    Prev    Next    Page+10