**Array –**array is a linear data structure which stores same data elements into contiguous way.

Let’s say,we have an array of 4 elements.The elements are 22, 34,-65, 8. Stored elements into array cells or block.

So,size/length of the array will = 4.

The graph of array will –

Elements | 22 | 34 | -65 | 8 |

Index | 0 | 1 | 2 | 3 |

**Declaring an array**

An array can be declared in many ways. See below most common two ways –

int a[] = new int[4]; a[0]=22; a[1]=34; a[2]=-65; a[3]=8; |

int a[] = {22,34,-65,8};

Or,

int a[] = {22,34,-65,8};

**Let’s make some programs about array declaration and displays array elements.**

public class arrays { public static void main(String[] args) { // declaring an integer array-> ‘a’ with size=4 int a[] = {22,34,-65,8}; // displaying array elements through for loop for(inti=0;i<4;i++){ System.out.println(a[i]); } } } |

**output**

public class array1 { public static void main(String[] args) { // declaring an integer array-> ‘a’ with size=4 int a[] = new int[4]; a[0]=22; a[1]=34; a[2]=-65; a[3]=8; // displaying array elements through for loop for(inti=0;i<4;i++){ System.out.println(a[i]); } } } |

**output**

**Remember:** if you declare an array and initialize some values of it, then other block/cells of array by default initialize with 0(zero). See below code, to understand this.

public class array2 { public static void main(String[] args) { // declaring an integer array-> ‘a’ with size=4 int a[] = new int[4]; a[0]=22; a[1]=34; // displaying array elements through for loop for(inti=0;i<4;i++){ System.out.println(a[i]); } } } |

**output**

public class array3 { public static void main(String[] args) { // declaring an integer array-> ‘a’ with size=4 int a[] = new int[4]; // displaying array elements through for loop for(inti=0;i<4;i++){ System.out.println(a[i]); } } } |

**output**

**Note: array can randomly accessed but a linked list can’t?**

Ans – In array, data elements get stored in a contiguous memory location. So when we get first index of an array, we can easily calculate the **address of anyelement**, by using that element index in array.

In the case of linked list, element don’t get stored into contiguous memory location. So accessing any particular element using its index, from any particular element index can’t possible.

**Array types**

Generally array are two types – one dimensional array and multi-dimensional array. Multi-dimensional array can be many types like – 2D,3D,4D etc.

Generally on basics of most usage, array treated as 3 types –

- 1D array – one dimension(like X)
- 2D array – two dimension(like X , Y)
- 3D array – three dimension(like X,Y,Z)

**Calculating address of 1D array element.**

B.A = base address/starting address, i = index of desired element, w = array block size (data-type size).

**If array index start with 0(zero),**

A[i] = i*w + B.A

**If array index start with 1,**

A[i] = (i-1)*w + B.A

**Problem 1: We assuming, we have an array of 5 integers arr[5] = (10, 20, 44, 55, 65). Starting (base) address = 200. Calculate the address of element ‘55’.**

Ans–each block of array size = 2 byte (assume integer size as 2 byte)

Base Address = 200. Starting index = 0*(when in question starting index not mentioned)*

Indexes of all elements will –

10 | 20 | 44 | 55 | 65 |

0 | 1 | 2 | 3 | 4 |

Desired element ‘44’, index is 2. So i = 2

So, according to formula:A[i] = i*w + B.A

arr[2] = 2*2 + 200 [i=2,w=2,B.A=200]

arr[2] = 204.

So, with index and address of elements array will look like –

10 | 20 | 44 | 55 | 65 |

0 | 1 | 2 | 3 | 4 |

200 | 202 | 204 | 206 | 208 |

**Problem 2: We assuming, we have an array of 4 integers arr[4] = (10, 20, 44, 55). Starting (base) address = 100. Calculate the address of element ‘55’. Starting index = 1.**

Ans – each block of array size = 2 byte (assume integer size as 2 byte)

Base Address = 100. Starting index = 1

Indexes of all elements will –

10 | 20 | 44 | 55 |

1 | 2 | 3 | 4 |

Desired element ‘55’, index is 3. So i=4

So, according to formula: A[i] = (i-1)*w + B.A

arr[4] = (4-1)*2 + 100 [i=4,w=2,B.A=100]

arr[2] = 106.

So, with index and address of elements array will look like –

10 | 20 | 44 | 55 |

1 | 2 | 3 | 4 |

100 | 102 | 104 | 106 |

*But wait a second, when your staring index with 3,4,5,6,7,8,…… n. then how you calculate address of any element?*

i = index of element, l.b = lower bound of array(starting index) , w=array block size, B.A=base address

**Universal formula: a[i] = (i – l.b)*w + B.A**

**Problem 3: We assuming, we have an array of 4 integers arr[4] = (10, 20, 44, 55). Starting (base) address = 100. Calculate the address of element ‘55’. Starting index = 5.**

Ans – each block of array size = 2 byte (assume integer size as 2 byte)

Base Address = 100. Starting index = 5,sol.b = 5.

Indexes of all elements will –

10 | 20 | 44 | 55 |

5 | 6 | 7 | 8 |

Desired element ‘55’, index is 8.i = 8

So, according to formula: A[i] = (I – l.b)*w + B.A

arr[8] = (8-5)*2 + 100 [i=8,w=2,B.A=100]

arr[2] = 106.

So, with index and address of elements array will look like –

10 | 20 | 44 | 55 |

5 | 6 | 7 | 8 |

100 | 102 | 104 | 106 |

**In array, we have different type of operations to perform.**

Some of them are –

- Insert new element into array
- Delete element from array
- Search any element in array
- Sort array by assessing or descending order

In array operations, you also get touch with traverses of elements.

But basically traverse basically an approach. There have a common difference between traverse and searching. Assume two cases, when your teacher says open a page in your book, but don’t follow the index page, so you need to traverse one by one page in book to follow the right content.

But if the teacher says, you to find the content from page’s index. So at first you see the index page, then you find the chapter; searching basically works these way.