Arrays can be thought of as homogeneous collection of variables of same type. Arrays are index based. The first index is 0,next 1 and so on till desired.We can have desired values in the desired index.While declaring arrays we need to specify the data type and name of the array.
int a; or
We can create an array using new operator.
Arrays in java can be single dimensional or multi-dimensional.
1.Single dimensional arrays
In the above example we have created an array using new.We have declared a variable x which creates an array of 6 elements of int type.
Thereafter we have initialized and printed the values.
Now instead of declaring and initializing them as above we can also do it as
We can print it as given in the above example,
Passing arrays to methods:
We can pass the array to method in java.
1 2 3
4 5 6
7 8 9
The Arrays class
The java.util.Arrays class contains various static methods for sorting and searching arrays, comparing arrays, and filling array elements.
public static int binarySearch(Object x, Object key)
This searches the specified array of Object ( Byte, int , double, etc.) for the specified value using the binary search algorithm. The array must be sorted prior to making this call. This returns index of the search key, if it is contained in the list; otherwise, (-(insertion point + 1).