You are on page 1of 3

SINGLE DIMENSIONAL ARRAYS

This is the simplest of array and is referred to as 1-D array. When


an array is declared, memory for number of elements of a
particular type is assigned to the array during compile time. This
allocation is called compile time allocation. This means that even
if the array is partially filled, the memory for the entire size will be
held by the array.
Initialization is the process of assigning a value to each array
element when it is first declared.
Declaration of one dimensional arrays
Every array declaration has :

A name or identifier with which it is referenced.

Size that determines the maximum number of elements that


the array can store at a time.

The data type determines the type of data that the array will
hold.

The individual elements are referred to using subscripts or indices


which show the position of the elements in the array. The
subscript or location ranges from position 0 to n-1 where n is the
size of the array.
Initialization of one dimensional arrays
Initialization is the process of assigning a value to each array
element pent when it is first declared. The individual elements are
referred to using subscripts or indices and show the position of
the element in the array. The subscripts or location ranges from
position 0 to n-1 where n is the size of the array.
Syntax :
Type identifier [] = {data values separated by commas};

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.

If the value of an index for an array element is negative, a


decimal, or greater than or equal to the length of array (because
the last subscript is array length 1), an error message will be
Array Index Out Of Bounds Exception.
If you see this message, immediately check to see how your array
is being utilized.
Using for loop to access arrays
Since the array elements lie in contiguous locations stored from
location 0 to length-1, it is very convenient to use for loops to
access entire arrays. When the number of elements are large in
number it is not practical to manipulate each element separately.
Input/Output data of 1-D array
Data can be input into an array using the same System in stream.
The following program inputs data into an array and then displays
data from two arrays. The string array is initialized with names at
the beginning.
Memory Representation of a 1-D array
An array is defined as a named set of elements of similar type
stored in contiguous locations. The memory address of the first
element is called the base address. The other element are
automatically stored in continuous locations depending on the
basic size of each element. The name of the array is the address
of the first element of the array.
Examples:
short Num [] = new short [5];
Base address here is 3000.
Float Price [] = new float [5];
Base address here is 5000
Each float element is of 4 bytes so the address is incremented by
4 bytes.s

You might also like