Unit 4 Arrays
Unit 4 Arrays
In order to store same type of value ,we need to create an array object [block of
memory]. Before we creating an object we need to create a block of memory to store the
address of the array object.
To store the homogenous type of values using array we need to create Array
reference variable and Array Object. To make use of array reference variable we can store
the address of Array object. We can create the array for both Primitive and Non – Primitive
Data type.
Characteristics of Array:
1. Array is a continuous block of memory which is used to store multiple value or multiple
data.
2. The array size must be specified at the time of Declaration
3. Once the array size is specified those many continuous blocks will created, once the
blocks will created later we can not increase or decrease the size of an array.
4. Hence array is a fixed length of memory.
5. Based on index values we can store the data in that block when the size of block will
specified.
6. Array block consist of index values.
7. The index of an array is start from zero to ends at array length-1.
8. Hence we can access the element or initialize the element into an array by using indexing
value.
Note: Every Array is an object in Java.
1. Declaring Array Variables
To use an array in a program, you must declare a variable to reference the array and
specify the array’s element type. Here is the syntax for declaring an array variable.
Syntax: data_type[ ] refVariable_name;
data_type refVariable_name[ ];
The element type can be any data type, and all elements in the array will have the
same data type. for example following conditions declared the variable ' a ' that reference an
array of different data type.
Example:
2D Array
In Java, a 2D array is an array of arrays, also known as a matrix. It consists of rows and
columns, forming a grid-like structure. Each element in a 2D array is accessed using two
indices: one for the row and another for the column.