Array

What is an Array?

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

Each item in an array is called an element, and each element is accessed by its numerical index.

Or a simpler code with same result:

Run The Application

Right click to the ArrayDemo class; select Run As -> Java Application.

Output

Copying Arrays

The System class has an arraycopy method that you can use to efficiently copy data from one array into another:

The two Object arguments specify the array to copy from and the array to copy to. The three int arguments specify the starting position in the source array, the starting position in the destination array, and the number of array elements to copy.

The following program copy 3 last elements (from element 2 and take 3 elements):

Run The Application

Right click to the ArrayCopyDemo class; select Run As -> Java Application.

Output

Multi dimensional array

You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values.

Run The Application

Right click to the MultiDimentionalArray class; select Run As -> Java Application.

Output