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
- Single Dimensional Array
- Multidimensional Array
Array Declaration
Single dimension array declaration
Syntax
- int[] a;
- int a[];
- int []a
Multidimensional Array declaration
Syntax
- int[][] a;
- int a[][];
- int [][]a;
- int[] a[];
- int[] []a;
- int []a[];
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