1

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 the array is 10, but how do I find which index contain the last value and after that is empty? Am I suppose to do a loop and stop at index == null?

I mimic an arraylist by create a dynamic array that grow when the size is full.

Arg, forgot to tell you guys, if the array is int, then the empty slots will be 0?

12
  • 1
    You should use an ArrayList instead Commented Oct 14, 2012 at 9:37
  • 1
    Do you want to iterate the array?? Or I understand something wrong? Ok, I got it wrong.. But you should definitely use an ArrayList Commented Oct 14, 2012 at 9:37
  • 2
    You keep track of it, that's how. Don't go relying on null's - what if you put a null in it? It'd disappear. Commented Oct 14, 2012 at 9:39
  • that is my assignment... use normal array and make a dynamic that have a method that make a bigger array when the capacity is filled up.... I not allowed to use array list guys... Commented Oct 14, 2012 at 9:40
  • @RyokoNela You cannot have tradional array behave like a dynamic one.. It has a fixed size.. That cannot be changed.. Commented Oct 14, 2012 at 9:42

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

As I said I making something implement the same principle that apply for arraylist... that why I need all this stuffs.
2

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

3 Comments

Can I declare the checking variable as local for the add method? I am not allow to add more class fields. Wait, actually the specs dont forbid me from adding more... look like I can add a class field.
if you made it local it will be set to zero (because is essence you will have a new instance of this variable) everytime you call the add method so it will not work, If you can't add class fields then I don't see other option that to iterate the array to check if it filled before you add a new element. Strange though that you are not allowed to add class variables.
If you cannot add the variable then a trick is to check if the array is filled is to check that the last item is not null (or zero) so a check if (array[size] != null) could help assuming that: you add elements in order (from 0 to size without skipping elements) and null (or 0 in case of int array) is not a valid number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.