EXAMPLE FOR DEFINING AN ARRAY:
The dataType can be primitive or class types.
Every member of an array will have the same type.
The array itself is immutable but mutability of its elements depends on the element type.
The size(s) of an array is (are) defined when allocating in memory, then not changeable.
The 1st element has the 0th index.
EXAMPLE FOR DECLARATION, MEMORY ALLOCATION AND CONSTANT:
EXAMPLE FOR ITERATION THROUGH 1D ARRAY:
Accessing an element with wrong index (<0 or >=length or the array has 0 length) throws exception.
For arrays, .length is a property, not a method (length is fixed after memory allocation).
Using class element type (like String or Method) means the element will contain a reference to such object.
EXAMPLE FOR DECLARATION AND MEMORY ALLOCATION:
A 2D array is a 1D array by rows where in every row there is an independent 1D array of arrayType.
As a result, the 1D array length in every row can be different but not the element (cell) type.
EXAMPLE FOR ITERATION THROUGH 2D ARRAY:
EXAMPLE FOR 2D ARRAY CONSTANT:
EXAMPLE FOR 3D ARRAY CONSTANT:
In case of multi-dimensional arrays the rules are the same.
Usage is rare since hardship to follow indexing.
In science can have advantages.
Shallow copy means only the reference to the original array will be copied.
In case there is a change in the copy, the original array will change as well.
EXAMPLE FOR SHALLOW COPY OF ARRAY:
Deep copy means a new reference will be created for the new array.
Any changes in the new one will not change the original array.
EXAMPLE FOR DEEP COPY OF ARRAY WITH ITERATION:
In case the element type is a class, the element will be a reference to an object. That means from different arrays elements can show to the same memory allocation.
In case the object is immutable, any changes to the element will not change the object in memory but create a new object instead, and the array element will show to that so the change will not be copied to the original array.
In case the object is mutable and changed, the reference will not change and the change itself can be reached from the original array.
Some immutable object types: String, array.
Some mutable object types: ArrayList, StringBuilder (rarely used as element type of arrays).
Due to the nature of this type of deep copying, the elements can be processed and-or filtered inside the iteration.
EXAMPLE FOR DEEP COPY OF ARRAY WITH ARRAYCOPY:
Best solution for 1D arrays.
For >1D cases, must use (nested) loops until the last dimension is reached - for that the arrayCopy is applicable.
EXAMPLE FOR DEEP COPY OF ARRAY WITH COPY AND COPYOFRANGE:
For >1D cases, must use (nested) loops until the last dimension is reached - for that the copyOf() and copyOfRange() is applicable.
EXAMPLE FOR CONCATENATION WITH ITERATION:
EXAMPLE FOR CONCATENATION WITH ARRAYCOPY:
1D solution.
EXAMPLE FOR TOSTRING:
>1D solution.
EXAMPLE FOR DEEPTOSTRING:
EXAMPLE FOR SORT IN ASCENDING ORDER, PRIMITIVE TYPES:
EXAMPLE FOR SORT IN ASCENDING ORDER, OBJECT TYPES:
EXAMPLE FOR SORT IN DESCENDING ORDER, PRIMITIVE TYPES:
EXAMPLE FOR SORT IN DESCENDING ORDER, OBJECT TYPES:
There is no ready-to-use method in the array class nor in the Arrays class. However, a sequential search can be made.
The 2nd part of the condition can be modified to fit to the search key type.
EXAMPLE FOR SEARCH IN RANDOM-ORDERED ARRAY:
In case the array is sorted, this method will provide an index, or if found none, a negative number.
In case there is more than one of element matching with the search key, undefined which will have the index pointed on.
Can be used for primitive types or object types which has .equals() method.
EXAMPLE FOR SEARCH IN SORTED ARRAY:
The List<...> list = Arrays.asList(array) formula can have different outcome:
in case the array element type is a class (or wrapper), the resulted element type will be the same (e.g. List<Integer>)
in case the array element type is a primitive, the resulted element type will be the array type as a class (e.g. List<int[]>)
EXAMPLE FOR CONVERTING CLASS-TYPED ARRAY TO ARRAYLIST:
EXAMPLE FOR CONVERTING PRIMITIVE-TYPED ARRAY TO ARRAYLIST:
EXAMPLE FOR CONVERTING ARRAYLIST TO CLASS-TYPED ARRAY:
EXAMPLE FOR CONVERTING ARRAYLIST TO PRIMITIVE-TYPED ARRAY:
The List<...> list = Arrays.asList(array) formula can have to different outcome:
in case the array element type is a class (or wrapper), the resulted element type will be the same (e.g. List<Integer>)
in case the array element type is a primitive, the resulted element type will be the array type as a class (e.g. List<int[]>), therefore boxing/unboxing is necessary
EXAMPLE FOR CONVERTING TO HASHSET AND BACK:
EXAMPLE FOR CREATE AND FILL AN ARRAY: