I think what you want to do here is this:
int getLength(int[] arr)
{
int length = 0;
for(int i = 0; i < arr.length; i++)
length++;
return length;
}
I am assuming that this is for some sort of homework, and this is just for teaching for loops because this method is complete overkill as you can get the length of an array with the arr.length
. eg.
int getLength(int[] arr){ return arr.length; }
solved Can somebody help me with array code in java [closed]