General Orientation and Introduction To Arrays LECTURE
General Orientation and Introduction To Arrays LECTURE
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.
Normally, an array is a collection of similar type of elements which has contiguous memory location.
Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory
location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on 1st index and so on.
Advantages
o Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
o Random access: We can get any data located at an index position.
Disadvantages
o Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection
framework is used in Java which grows automatically.
Anything having one-dimension means that there is only one parameter to deal with. In regular terms, it is the length of something. Similarly, as far as
an array is concerned, one dimension means it has only one value per location or index.
MULTIDIMENSIONAL ARRAY
The Java multidimensional arrays are arranged as an array of arrays i.e. each element of a multi-dimensional array is another array. The representation
of the elements is in rows and columns. Thus, you can get a total number of elements in a multidimensional array by multiplying row size with column
size.
So if you have a two-dimensional array of 3×4, then the total number of elements
in this array = 3×4 = 12.
Two-Dimensional Array
The simplest of the multi-dimensional array is a two-dimensional array. A simple definition of 2D arrays is: A 2D array is an array of one-dimensional
arrays.
In Java, a two-dimensional array is stored in the form of rows and columns and is represented in the form of a matrix.
So if you have an array of 3×3, this means it will have 3 rows and 3 columns.
As shown above, each intersection of row and column stores an element of the 2D array. So if you want to access the first element in the 2d array, then it
is given by [0, 0].
Note that as the array size is 3×3, you can have 9 elements in this array.
Three-Dimensional Array
We already discussed that an array gets more complex as their dimensions increase. Three-dimensional arrays are complex for multi-dimensional arrays. A
three dimensional can be defined as an array of two-dimensional arrays.
– The number of Tables/Arrays: The first dimension indicates how many tables or arrays a 3d array will have.
– The number of Rows: The second dimension signifies the total number of rows an array will have.
– The number of Columns: The third dimension indicates the total columns in the 3d array.