π What is it?
Learners believe that the length of an array an only be accessed using a fixed number, rather than using the .length property.
For example, instead of writing:
for (int i = 0; i < values.length; i++) {
println(values[i]);
}
they may write:
for (int i = 0; i < 5; i++) { // hardcoded length
println(values[i]);
}
This leads to code that only works correctly as long as the array has exactly the hardcoded length, and breaks otherwise.
π οΈ Why is it incorrect?
Hardcoding array lengths makes the code fragile and less flexible. If the array changes in size (e.g. because of different initialization or because it’s replaced), the loop may no longer behave correctly and may throw runtime errors such as ArrayIndexOutOfBoundsException.
The correct way to access all elements of an array is the use of the built-in .length property, which always reflects the actual size of the array:
int[] values = { 3, 5, 7, 9 };
for (int i = 0; i < values.length; i++) {
println(values[i]);
}
This ensures that the loop adjusts automatically, no matter how long the array is.
π§© Typical errors
- Using a fixed number instead of
.length:
int[] data = new int[10];
for (int i = 0; i < 10; i++) {
println(data[i]);
}
- Failing to adapt the loop when the array size changes:
//int[] test = { 1, 2, 3 };
int[] test = { 1, 2, 3, 4 };
for (int i = 0; i < 3; i++) {
println(test[i]);
}
π± Origin
This misconception may arise from early examples in which arrays are always shown with a fixed, known size. As a result, learners may develop the belief that the array length must or can be repeated manually. Simplified code examples that use constant values instead of the .length property tend to reinforce this misconception.
In addition, some learners may feel insecure when working with object properties in Processing, particularly when using dot notation like array.length, which may seem unfamiliar or confusing at first.















