In the final section of arrays, we will discuss;
o The Length variable and the Length()(i.e. Length method).
o Anonymous Arrays.
o Array Element Assignments.
o Array Variable Assignments.
o Length Vs Length()
The statements below describe their uses, and examples to back-up follows;
• Length variable is a final variable applicable for arrays, and length variable represents the size of the array.
• We cannot apply the “length()” to the size of an array, else you will get a compile time error.
• The length() is a final method applicable for only string objects and it returns the number of characters present in the string given.
Note: Length Variable is applicable for arrays, but not for string objects, whereas the length() is applicable for string objects, but not for arrays, as clearly stated above.
The first invalid is because, the length() is not applicable for any array size and this clearly includes the string array size, whereas the latter is because the length variable is not applicable for string objects and this clearly includes string object elements within an array as well, this is a good approach to summarize all we have discussed leading to the last example.
• In multi-dimensional arrays, length variable represents only base size, but not total size.
• There is no direct way to find the total length of a multi-dimensional array, but indirectly, we can determine it as follows;
o Anonymous Arrays
Sometimes we can declare an array without a variable name, such type of nameless arrays is called anonymous arrays.
The main purpose of anonymous arrays is for instant use (i.e. one-time usage).
We have defined an anonymous array and also stated its purpose, the next thing would be to create one, and then see how it works.
There are some rules guiding anonymous arrays, as follows;
• While creating anonymous arrays, we can’t specify the size, otherwise, we will get a compile time error.
• It is completely valid and possible to create a multi- dimensional anonymous array.
• Based on our requirements, we can give a variable name to an anonymous array, but doing this will make the array no longer anonymous.
o Array Element Assignment
• Case 1; Primitive Type Arrays
In the case of primitive type arrays as array elements, we can assign any type in regard to the data type that is compatible with it, which can be implicitly promoted to the declared type.
Below is the progressive data type assignment diagram.
Further-more; in the case of float-type arrays, the allowed data types are; byte, short, char, int, long and float.
• Case 2; Object Type Arrays
In the case of object type arrays, as array elements, we can provide either a declared type object or it’s child class object.
• Case 3; Interface Type Arrays
For interface type arrays, as array elements, the array implementation class objects are allowed.
o Array Variable Assignments
• Case 1:
Element level promotions are not applicable at array level.
As seen above, char data types can be promoted to int types, whereas a char array cannot be promoted to an int array.
• Case 2:
Whenever we are assigning one array to another array, internal elements won’t be copied, just reference variables will be re-assigned.
• Case 3:
When we assign an array to another array, the dimensions must be matched.
In the case of a one-dimensional array, we should assign a one-dimensional array only, if we try to assign any other dimension, then we will get a compile time error.
Note; Whenever we are assigning one array to another, both dimensions and types must be matched, but sizes are not required to match (i.e. internal elements).