Java Theory
Arrays
An array is a group (or collection) of variables of the same data types. Each value is called an element of the array. An array can be of any type, for example int, char, float ..etc. The first element of an array starts with index zero.
Array Declaration
Declaration of Array
Syntax |
---|
data_type[]array_variable; Ex: int[] a; Or data_type[]array_variable; Ex: int a[]; int a[] = new int[5]; |
Initialization of an Array
Syntax |
---|
int[] a[4] = new int[5,9,4,15]; |
Initialization One Dimensional
Arrays can be initialized at declaration time in this source code as:
Syntax |
---|
int[] age[5] = {5,9,4,15}; |
Multi Diemnsional Array
The simplest form of multi dimensional array is a 2-dimensional array. It is also known as matrix. To declare multi dimensional integer array of size [x][y]. which means x is number of rows, y is number of columns can be shown in fig.
Syntax |
---|
data type[] array_variable; Or data type array_variable[][]; int a[][] = new int[3][3]; //3rows and 3 columns int a[][] = {{1,2,3}, {4,5,6}}; |