Array In Java

What is an Array?
1. An array is a collection of similar type of elements that have a contiguous memory location.
2. The variables in the array are ordered and each have an index.

Java Array
An Array Example
Array Types:

1. Single Dimensional Array
2. Multidimensional Array

Advantages -  
code optimization by storing data and we can retrieve or sort the data efficiently.
We can get any data located at an index position.

Disadvantages-
Stores similar type of data
We can store only the fixed size of elements in the array. It doesn't grow its size at runtime.

1-D Array:

Declaration of an Array:
Syntax: data_type array-name[]; Example: int a[];
 or
Data_type[] array-name;  Example: int[] a;

Instantiation of an Array
array-var-name =new datatype[size];  Example: a= new int[5]

Declaration and Instantiation
Data_type array-name[]=  new data_type[5];
int a[]=new int[5];

Array Literal
Data_type array-name[] = {<Values>}
Example: int a[] = {12,23,1,34,5,6}

2-D Array:

Declaration of an Array:
Syntax: data_type array-name[][]; Example: int a[][];
 or
Data_type[][] array-name;  Example: int[][] a;

Instantiation of an Array
array-var-name =new datatype[][];  Example: a= new int[2][2]

Declaration and Instantiation
Data_type array-name[][]=  new data_type[2][2];
int a[][]=new int[2][2];

Array Literal
Data_type array-name[] = {<Values>}
Example: int a[][] = {{12,23},{23,34}}

To get more details please watch below youtube video and Subscribe the channel.



No comments:

Post a Comment