CJP Unit-3
CJP Unit-3
Unit-03
Methods and Arrays
Array
Method
Array
An array is a collection of similar type of elements that have contiguous memory location and
shares a common name.
Syntax : data_type variable_name[] = new type[size_of_array];
Example : int a[] = new int[10];
35 13 28 106 35
a 42 5 83 97 14
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
The data_type specifies the type of the elements that can be stored in an array, like int, float, char etc...
The size_of_array indicates the maximum number of elements that can be stores inside the array.
In the example, data type of an array is int and maximum elements that can be stored in an array are 10.
Important point about Java array.
An array is derived datatype.
An array is dynamically allocated.
The individual elements of an array is refereed by their index/subscript value.
The subscript for an array always begins with 0.
Prof. Arjun V. Bala #3140705 (OOP-I) Unit 03 – Methods and Arrays 3
One-Dimensional Array
An array using one subscript to represent the list of elements is called one dimensional array.
A One-dimensional array is essentially a list of like-typed variables.
Array declaration: type var-name[];
Example: int student_marks[];
Above example will represent array with no value (null).
To link student_marks with actual array of integers, we must allocate one using new keyword.
Example: int student_marks[] = new int[20];