Arrays in JAVA

Arrays in JAVA

An array in JAVA is a data structure which has a collection of multiple elements of the same data type(int, float, etc) stored in consecutive(contiguous) memory locations.An array always starts with index 0.

Array programs have been given below.

Why to use array?

Take an example, you want to store a 1000 numbers then you will need 1000 variables to do so, with an array you will need only one variable of size 1000, hence it is efficient to store and access data.

All elements in an array are indexed hence to sort, search, retrieve elements in array is easy.

Syntax

dataType variable[] = new dataType[size of array]     eg   int arr[] = new int[8]

OR

dataType[] variable = new dataType[size of array]     eg   int[] arr = new int[8]

Types of Array in JAVA?

  1. Single Dimensional Array  (int[] a = new int[4])
  2. Multi Dimensional Array (int[][] a = new int[3][2])

 

Share Me!
  •  
  •  
  •  
  •  
  •  
  •