KnowledgeBoat Logo
LoginJOIN NOW

Computer Applications

What is an array ? What is the need for arrays ?

Java Arrays

1 Like

Answer

An array is a collection of variables of the same type that are referenced by a common name. It is a reference data type which stores data values in contiguous memory locations. An array is declared and initialized as follows:

int arr[] = new int[10];

The use of arrays have the following benefits:

  1. Easy to specify — The declaration, allocation of memory space, initialization can all be done in one line of code.
  2. Free from run-time overheads — There is no run-time overhead to allocate/free memory, apart from once at the start and end.
  3. Random access of elements — Arrays facilitate random (direct) access to any element via its index or subscript.
  4. Fast Sequential Access — It is usually faster to sequentially access elements due to contiguous storage and constant time computation of the address of a component.
  5. Simple code — As arrays facilitate access of multiple data items of same type through a common name, the code becomes much simpler and easy to understand.

Answered By

1 Like


Related Questions