int arr[] = new int[10];
int size=0;
while(???)
{
i++;
}
System.out.println(size); // Should print 10
How can I loop over the array without using arr.length or other library functions?
int arr[] = new int[10];
int size=0;
while(???)
{
i++;
}
System.out.println(size); // Should print 10
How can I loop over the array without using arr.length or other library functions?
int arr[] = new int[100]; int sum = 0; int i = 0; while (true) { try { sum += arr[i]; } catch (ArrayIndexOutOfBoundsException e) { break; } i++; } System.out.println("Array is of size " + i);
I'm assuming array is of ints, but the idea is the same.
Under the hood you'll get a .length (unless you use a strange compiler), but since the question's kinda weird...
int[] array = new int[100];
int size = 0;
for(int i : array){
++size;
}
System.out.println("Size: " + size);
I still don't understand the point