So let say I have an array of size 10 with index range from 0 to 9. I add a bunch of elements in and stop adding at index 6. So with array.length, I can know that the size of th
Use java.util.ArrayList
. There you no need to think about index and it is resizable-array implementation.
At the time of array creation by default all values are null so if you do not insert any value at any index (may be at end or beginning or middle of array) it would be just null. So you should put null check to verify.
Since this is you assignment a trick is to add a variable to follow the number of elements added.
So you can have a public int size = 0
variable and change you add and remove operations to increase and decrease this variable whenever you add or remove an element.
Then in you add method you can have a simple check to see if you need to expand the array
if (size == array.length)
expandArray