Friday, August 11, 2017

Arrays


Array is a collection of similar type of data. It is fixed in size means that you can't increase the size of array at run time. It is a collection of homogeneous data elements. It stores the value on the basis of the index value.



Advantage of Array

1.      Code Optimization: It makes the code optimized, we can retrieve or sort the data easily.

2.      Random access: We can get any data located at any index position.

Disadvantage of Array

1.      Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at runtime.



Types of Array

  1. Single Dimensional Array
  2. Multidimensional Array

 Array Declaration

Single dimension array declaration

Syntax

  1. int[] a;
  2. int a[];
  3. int []a

Multidimensional Array declaration

Syntax

  1. int[][] a;
  2. int a[][];
  3. int [][]a;
  4. int[] a[];
  5. int[] []a;
  6. int []a[];
Array creation

Every array in a Java is an object, Hence we can create array by using new keyword.

Syntax

int[] arr = new int[10]; // The size of array is 10.

or

int[] arr = {10,20,30,40,50};

Accessing array elements

Access the elements of array by using index value of an elements.

Syntax

arrayname[n-1];

every array type has  its corresponding class .

Class arr

{

Public static void main(String[] args)

{

Int[]  x=new int[3];

S.O.P(x.getClass().getName());// [I

}

}

No comments:

Post a Comment