Document For Programming in JAVA
Document For Programming in JAVA
The data type determines the type of data that the array will
hold.
Example :
Int A[] = {10, 20, 30, 40, 50}
This means that while declaration itself the memory is allocated
and each array element is given a value. From the diagram below
you can see the effect of the declaration and initialization
statement.
More Examples
Double Temp [] = {15.5, 20.25, 18.5, 7.25};
Char N[] = {C, h, a,r,I,e,s};
Accessing Array Elements
Each item in an array is called an element, and each element is
accessing by its numerical index. The individual elements can be
accessed using the array name and the index or subscript within
brackets. Take the case of the same array as before.
int A[] = {10, 20, 30, 40, 50};
Syntax :
Array name [index]
Each element can be accessed or initialized using the following
method:
A[0] = 10; A[1] = 20; A[2] = 30; A[3] =40; A[4] = 50;
Array Length & Exceptions
When dealing with array, the number of elements contained
within the array is the arrays length. This length can be
obtained by using the array name followed by the length. If an
array named Data contains 10 values, the code Data. Length
will be 10. The length of an array is the number of elements in
the array, which is one more than the largest subscript. The
length is a data member and not a method . So it must not have
brackets() at the end.