If I have an Array Candy[] type;
and type = new Candy[capacity];
if the Array is full is capacity = type.length
?
Since you are using array, the size of array is determined during compilation. Thus if your intention is to check whether current array's index has reached the last array element, you may use the following condtion (possibly in a loop) to check whether your current array index is the last element. If it is true, then it has reached the last element of your array.
Example:
int[] candy = new int[10]; //Array size is 10
//first array: Index 0, last array index: 9.
for (int x=0; x < candy.length; x++)
if (x == candy.length - 1)
//Reached last element of array
You can check the size of the array by using:
candy.length
You check whether it is last element by using:
if (currentIndex == candy.length - 1) //Where candy is your array
Make sure you are using double equal ==
for comparison.
Single equal =
is for assignment.
Java creates an array with capacity
count of references to the Candy
instances initializing array with the null
s. So any array in java is full and
type.length == capacity
is always true.
type = new Candy[capacity]
is equivalent to
type = new Candy[] {null, null, null, /* capacity count of nulls */, null};
In the example you show type.length
will always be equal to capacity
(after the initialization). Also your array will always have capacity
elements but they will initially all be null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With