Introduction to java arrays
In this section you will be introduced to the concept of Arrays in Java Programming language. You will learn how the Array class in java helps the programmer to organize the same type of data into easily manageable format.
Program data is stored in the variables and takes the memory spaces, randomly. However, when we need the data of the same type to store in the contiguous memory allocations we use the data structures like arrays. To meet this feature java has provided an Array class which abstracts the array data-structure.
The java array enables the user to store the values of the same type in contiguous memory allocations. Arrays are always a fixed length abstracted data structure which can not be altered when required.
The Array class implicitly extends java.lang.Object so an array is an instance of Object.
Structure of Arrays
Now lets study the structure of Arrays in java. Array is the most widely used data structure in java. It can contain multiple values of the same type. Moreover, arrays are always of fixed length i.e. the length of an array cannot be increased or decreased.
Lets have a close look over the structure of Array. Array contains the values which are implicitly referenced through the index values. So to access the stored values in an array we use indexes. Suppose an array contains "n" integers. The first element of this array will be indexed with the "0" value and the last integer will be referenced by "n-1" indexed value.
Presume an array that contains 12 elements as shown in the figure. Each element is holding a distinct value. Here the first element is refrenced by a i.e. the first index value. We have filled the 12 distinct values in the array each referenced as:
The figure below shows the structure of an Array more precisely.
As we declare a variable in Java, An Array variable is declared the same way. Array variable has a type and a valid Java identifier i.e. the array's type and the array's name. By type we mean the type of elements contained in an array. To represent the variable as an Array, we use  notation. These two brackets are used to hold the array of a variable.
By array's name, we mean that we can give any name to the array, however it should follow the predefined conventions. Below are the examples which show how to declare an array :-
It is essential to assign memory to an array when we declare it. Memory is assigned to set the size of the declared array. for example:
Here is an example that creates an array that has 5 elements.
After declaring an array variable, memory is allocated to it. The "new" operator is used for the allocation of memory to the array object. The correct way to use the "new" operator is
Here, the new operator is followed by the type of variable and the number of elements to be allocated. In this example  operator has been used to place the number of elements to be allocated.
Lets see a simple example of an array,
In this example, a variable
Output for the given program:
We have already discussed that to refer an element within an
array, we use the  operator. The  operator takes an "int" operand and returns the element at that
index. We also know that the array indices start with zero, so the first element
will be held by the 0 index. For Example :-
Most of the times it is not known in the program that which elements are of interest in an array. To find the elements of interest in the program, it is required that the program must run a loop through the array. For this purpose "for" loop is used to examine each element in an array. For example :-
Here, we have taken an array of months which is,
String months =
Now, we run a for loop to print each element individually starting from the month "Jan".
for (int i = 0; i < months.length; i++ )
In this loop int i = 0; indicates that the loop starts from the 0th position of an array and goes upto the last position which is length-1, i < months.length; indicates the length of the array and i++ is used for the increment in the value of i which is i = i+1.
So far we have studied about the one-dimensional and
two-dimensional arrays. To store data in more dimensions a multi-dimensional
array is used. A multi-dimensional array of dimension n is a collection of items.
These items are accessed via n subscript expressions. For example, in a language that supports it, the element of the two-dimensional array x is
denoted by x[i,j].
int x = new int;
The expression x[i] is used to select the one-dimensional array; the expression x[i][j] is ued to select the element from that array. The first element of this array will be indexed with the "0" value and the last integer will be referenced by "length-1" indexed value. There is no array assignment operator.
Two-dimensional arrays are defined as "an array of
arrays". Since an array type is a first-class Java type, we can have an array of ints, an array of Strings, or an array of Objects. For example, an array of ints
will have the type int. Similarly we can have int, which represents an "array of arrays of ints". Such an array is said to be a two-dimensional array.
int A = new int
declares a variable, A, of type int, and it initializes that variable to refer to a newly created object. That object is an array of arrays of
ints. Here, the notation int indicates that there are 3 arrays of ints in the array A, and that there are 4 ints in each of those arrays.
Notice in the following example how the rows are handled as separate objects.
In this example, "int a2 = new int;" notation shows a two-dimensional array. It declares a variable a2 of type int,and it initializes that variable to refer to a newly created object. The notation int indicates that there are 10 arrays of ints in the array a2, and that there are 5 ints in each of those arrays.
After learning all about arrays, there is still one interesting thing left to learn i.e. copying arrays. It means to copy data from one array to another. The precise way to copy data from one array to another is
Thus apply system's arraycopy method for copying arrays.The parameters being used are :-
The following program, ArrayCopyDemo(in a .java source file), uses arraycopy to copy some elements from the copyFrom array to the copyTo array.
In this example the array method call begins the copy
of elements from element number 2. Thus the copy begins at the array element
'c'. Now, the arraycopy method takes the copie element and puts it into
the destination array. The destination array begins at the first element
(element 0) which is the destination array copyTo. The copyTo copies 5
elements : 'c', 'd', 'e', 'f', 'g'. This method will take "cdefg" out
of "abcdefghij", like this :