JAVA Arrays

  • An array is a collection of data of the same type stored in a contiguous memory location.
  • The array is the derived data type in the java programming language which can store the primitive data types, such as pointers structure, etc.

Advantages:

1) Code Optimization: Less code to access the data.

2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.

3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.

4) Random Access: We can access any element randomly using the array.

Disadvantages:

1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can’t exceed the limit. So, it doesn’t grow the size dynamically like Linked List which we will learn later.

Properties of Arrays:

  • Each element of an array is of the same data type and carries the same size.
  • Elements of an array are stored in contiguous memory locations where the first element is stored at the smallest memory location
  • Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element.

Array Declaration:

syntax:

data_type array_name [arraySize];

 Example:

int a[5];

Initializing Array:We can initialize an array during the declaration:

int num[5] = {1,2,3,4,5};

we can also initialize an array like this:

int num[] = {1,2,3,4,5};

Here, we haven’t specified the size however the compiler knows its size is 5 as we are initializing it with 5 elements.
Initializing Example:
  Accessing elements of the array: 

  • once the array is  declared, then the individual elements in the array can be referred to by the number in the brackets following the array name this number specifies the position of the element in the array 
  • Subscript starts with 0, which means arr[0] represents the first element in the array arr. 
  • Thus num[2] is not the second element but this is considered as the third element of the array.

For example:

int mydata[20];
mydata[0] /* first element of array mydata*/
mydata[19] /* last (20th) element of array mydata*/

Types of arrays:1. One-dimensional array1. Two-dimensional array 

One-dimensional array:

One dimensional array is an array that has only one subscript specification that is needed to specify a particular element of an array. A one-dimensional array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value. 

Syntax:

data-type arr_name[array_size];

Two Dimensional Array:

The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure. It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required.

syntax:

syntax:-data_type array_name[rows][columns];