I propose that Java enables programmers to overload of the indexing operator
[].
The indexing operator is currently restricted to Java arrays.
For example, elements of a Java array of floats are addressed straighforwardly:
float x[] = new float[10]; // using native Java arrays
float y[] = new float[10];
...
y[i] = (-x[i+1] + 2*x[i] - x[i-1]) / 4.0;
The code is very readable and concise. Consequently, it is easy to maintain.
In contrast,
any array-like object that does not use Java arrays must provide
functions to address its array elements. Such verbose function calls
are not warranted by the index operator's simplicity:
Myarray x = new Myarray(10); // using Java's Vector class
Myarray y = new Myarray(10);
...
y.setElement(i,(- x.ElementAt(i+1)
+ x.ElementAt(i )
- x.ElementAt(i-1)) / 4.0);
Why not always using the built-in Java array? Obviously, there is a need for additional array-like classes. JGL and the Java built-in vector class offer arrays that can be dynamically allocated. In my own research, I would like to write an out-of-core array class.
Arrays are fundamental to many applications, not only in science and engineering. By limiting the index operator to the Java built-in array, the language designer leaves the programmer with a no-win choice: either the programmer accepts verbose functional calls for simple element addressing or the programmer gives up the freedom to represent his array-like data structures in any way but as a Java built-in array.