Single & Multi-Dimensional Arrays in Java
Single & Multi-Dimensional Arrays in Java
Through this blog we
are going to learn about java array. Many important concepts like single
dimensional arrays, multi-dimensional arrays along with their working will be
explained in this blog.
·
What are Arrays?
An
array is a data structure, which can store a fixed- size collection of elements of the same
data type.
An array could a
collection of comparable kind of
elements which have contiguous memory locations. Array contains the elements
which are homogenous in nature. Arrays are capable of storing
one or more values of a specific data type and also supply indexed access to
store the same. Any element in an array is accessed by a number assigned
to it according it’s position which is known as its index. Arrays are the best
way to store the related information together.
· Arrays in java:
Java array is an object
which contains elements of same data
type. Also, the elements of an array are stored in a contiguous memory locations,
i.e is one after the other. We are able to store only a fixed set of elements
in a Java array. Arrays are dynamically allocated in java.
·
Dynamic Array in Java
The limitation of arrays
is that they are fixed in size. It means
we must specify the quantity of elements while declaring the array.
A
question arises that what if we wish to insert a component and there is no more
space is left for the new element? Here, the concept of a dynamic array comes
into consideration. It expands the dimensions of the array dynamically.
The dynamic array may be a variable size list
organization. It grows automatically once we attempt to insert a component if
there is no more extra space left for the new component. It allows us to add
and remove elements. A dynamic array is capable of allocating the memory during
runtime using heap. It can change its size during run time.
Working
of Dynamic Array
In the
dynamic array, the components are stored contiguously from the starting of the
array and the remaining space remains unused. We are able to add the elements
until the reserved spaced is totally used. When the reserved space is consumed
and required to feature some elements. In such a case, the fixed-sized array must
be increased in size.
Types of
arrays:
- Single Dimensional Array
Single dimensional array consists of one row or one column of
array elements that share a typical name and are recognized by their index
values that are assigned to them.
For example, consider this , a single dimensional array can be
used to store the marks scored by a student in five subjects, since this marks
can be written in one row or one column.
This marks can be accessed using the index assigned to them by writing it in
square brackets preceded by the name of that array.
Here’s how to declare and create single-dimensional
array java.
int[] marks; //Declaration!
marks = new int[5]; //Memory allocation!
int[] marks = new
int[5]; //Declaration + Memory allocation!
int[] marks =
{100,70,80,71,98} // Declare + Initialize!
- Multidimensional Array
An
array with multiple rows and columns is known as a multi-dimensional array. A
multi-dimensional array is capable of holding more than one row and column.
Data is accessed using row column index. A matrix is a perfect example of a
multidimensional array.
Syntax:
datatype [1][2]…[nth dimension] arrayname = new
datatype[1][2]…[nth size];
Here’s how we can declare and create 2-dimensional
array java.
int [][] flats = new int[2][3]
int[][] b = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};



Nice✌
ReplyDeleteNicely done ,just add three dimensional too 👍
ReplyDeleteNice work!!
ReplyDelete