In programming, a series associated with objects which would be the exact same size and type. Each object within an array is known as an array element. For instance, you could have an array associated with integers or even an array associated with characters or even an array of anything which has a described data type.
- An Array is the indexed sequence of components or elements
- The indices are sequential, with the upper and lower bounds.
- The index provides direct access to the corresponding component.
The key features of an array tend to be:
- Each element has the same data type.
- The entire array is stored contiguously in memory.
Arrays may have greater than one dimension. A one-dimensional array is known as vector, a two-dimensional array is known as matrix.
< type> < arrayName>[< array_size>]
Ex. int Ar;
The array elements are all values of the type < type>.
The size of the array is indicated by < array_size>, the number of elements in the array.
< array_size> must be an int constant or a constant expression. Note that an array can have multiple dimensions.
Some examples of array declarations:
float prices = new float;
flags = new boolean;
char codes = new char;
Example of Array
Insert Component Algorithm for array
Insert val at position ins in a[left..right]; This is an O(n) algorithm.
- Check left <= ins <= right
- Copy a[ins..right-1] into a[ins+1..right]
- Copy val into a[ins]
Delete Component Algorithm for array
Delete the value at position del in a[left..right] ; This is an O(n) algorithm.
- Check left <= del <= right
- Copy a[del+1..right] into a[del..right-1]
- Make a[right] unoccupied
Some ArrayList methods
- boolean add(Object o) : Appends the specified element to the end of this Vector.
- Object get (int index) : Returns the element at the specified position in this Vector.
- String toString() : Returns a string representation of this Vector, containing the String representation of each element
- Int indexOf(Object elem) : Searches for the first occurence of the given argument, testing for equality using the equals method.
- Object remove(int index) : Removes the element at the specified position in this Vector.
- int size() : Returns the number of components in this vector.