Understanding the array data structure
As with most languages, Java has a built-in array data structure and does not require any imports or external libraries. As such, the array behaves as most arrays in other languages. The only difference is that to instantiate an array, you need the new
keyword. Here are the two ways to declare an array of 10 elements of type int
:
int[] quantities01 = new int[10]; int quantities02[] = new int[10];
The difference is where the empty square brackets are placed on the left-hand side. Placing them after the type is considered the Java way. Placing it after the identifier is thought of as the C-language way. Either syntax is fine.
In most programming languages, numbers can be either ordinal or cardinal. The length of the array as declared when we instantiate it is a cardinal—or count—number. In the examples so far, the length has been...