Each element in the array is the same size, making it easy to directly access any element of the array. Assume that we have an array of 10 elements stored at location 10000 and that each element of the array takes up 8 bytes. The array looks like this:
Arrays are called direct access data structures because the compiler can generate code to go directly to any element of the array by multiplying the index by the size of an element, and adding that to the base address.
To access element #0: (0 * 8) + 10,000 = 10,000
To access element #1: (1 * 8) + 10,000 = 10,008
To access element #2: (2 * 8) + 10,000 = 10,016
To access element #n: (n * 8) + 10,000
Since the process of multiplying two numbers (the array index and the element size) and adding the result to another (the array's base address) takes the same amount of time, regardless of the value of the array index, we can access any element of an array in the same amount of time that it takes to access any other element of the array. This is the definition of direct access.
Direct Access Data Structure: a data structure in which any element can be retrieved in the same amount of time.
To see a visualization of an array: http://www.cs.armstrong.edu/liang/animation/ArrayListAnimation.html(cannot watch in Chrome)
Although arrays are a basic data structure in any programming language, Java (and .NET) have provided a class to implement an array (and some methods to make the array easier to use). The class is called the ArrayList, and it can be used in a Java program by importing java.util.*.
If you use the simple array data type (and do not use the ArrayList class), you must:
Make sure you don't go off either end of the array (Array Index Out of Bounds error).
Make sure you don't try to put more data in the array than it can hold.
Manually push data items down the list if you need to insert an item in the middle.
Manually push data items up the list if you need to delete an item in the middle.
On the other hand, if you use the ArrayList class, the array list will:
Not allow you to access an element outside the bounds of the array.
Automatically re-size the array for you (it will double the size of the array) if you try to add an element to an array that is already full.
Take care of moving items down if you insert a new element in the middle of the array.
Take care of moving items up if you delete an element in the middle of the array.