0
What would be the values of theData starting from index 0, if theData: 3 5 5 2 7 (Assume the data type is Integer) item: 5 f
WHAT IS THE OUTPUT
2 RĂ©ponses
0
not only in java, indexing starts from 0, that means: the first item/number
0
Sara Hassn Just about every language uses index 0 for the first element in arrays/lists.
This is due to how computers allocate memory for arrays/lists.
A continuous block of memory is assigned to the list variable based on number of elements in list and memory size of the stored data type.
The list variable effectively points to the start of this memory block.
To quickly access any element in the list, the computer takes provided index, multiplies by size of data type, and adds that to starting point of the list to get the necessary memory address.
Since the list variable is already pointing at start of memory block (i.e. first element), you don't need to add anything to it to get first element.
EXAMPLE - if each integer took 4 bits
myNums[0] - address of myNums + (0 * 4bits) = myNums address = starting address of 1st list element
myNums[1] - address of myNums + (1 * 4bits) = myNums address + 4bits = starting address of 2nd list element