Question: We have discussed how to implement an array list using a dynamic array. The following code segment adds an element to an array list that

We have discussed how to implement an array list using a dynamic array. The following code segment adds an element to an array list that is implemented using a dynamic array. But, the code segment has a logical error in the code. Where is the error?
1 function add(i: int, e: E)
2 if i <0|| i > n:
3 throw IndexOutOfBoundsException()
4 if n == data.length:
5 resize(2* data.length)
6
7 for j in 0 to i:
8 data[j+1]= data[j]
9
10 data[i]= e
11 n = n +1
data is the dynamic array
i is the index in the array where the new element is added
e is the element to be added
n is the number of elements currently in the list
The resize method in line 5 doubles the size of the array data

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!