Arrays java
Arrays
==================================
1. Introduction:
indexed collection of homogenous data.
Advantage:
we
can represent the huge number of values / data using a single variable.
Disadvantage:
Arrays
are fixed in size.
==>
once array size fixed we can't increase or decrease the size of an array.
==>
if we are using arrays concept in advance we need know the size of an array. (this may not be
possible always).
2. Array
Declaration:
1D Array:
int[]
x; -----> Recommended to use
int x[];
int []x;
==> At the time of array declartion we
can't specify the size of an array.
int[6] x; ---> X--- Compile time error 😥😥😥
int[] x; 👌👌👌
2D
array Declartion:
int[][] x;
int
[][]x;
int
x[][];
int[] []x;
int[] x[];
int
[]x[];
which of the
following ara valid?
int[] a[],b; ====> a = 2, b = 1
int[] a[],b[];
====> a = 2, b = 2;
int[] []a,b;
====> a = 2, b = 2;
int[] []a,b[];
====> a = 2, b = 3; 👌👌👌
int[] []a,[]b;
====> Compile time error - 😥😥😥
3D array
declaration:
int[][][] a;
array creation:
--> in java every array is an object.
using new operator.
ex:
int[]
name = new int[6];
loopholes:
1) at the time of
array creation only we need to specify the size. otherwise we will get compile
time error.
int[] name = new int[6]; 👌👌👌👌👌
int[] name = new int[]; ==> CE 😥😥😥
2) int[] name = new int[0]; 👌👌👌👌👌
3) int[]
name = new int[-6]; ==> Run time Error --> negative array size exception 😥😥😥
4) To specify the
array size allowed data types are byte, short, int, char.
===> if trying to specify the any other
types then we will get compile time error.
int[] name = new int[10];
5) the maximum allowed array size in java
2147483647. this is the maximum int value.
int[] name = new int[2147483647]; 👌👌👌👌👌
int[] name = new int[2147483648]; 😥😥😥 --> compile time error int value is too
large
4. Array
Intilization:
int[] name = new int[10]; ---> intilized
with default values.
int[][] name = new int[2][3];
int[][] name = new int[2][];
System.out.println(name);
System.out.println(name[0]);----> null
System.out.println(name[0][0]); ==> 😥😥😥 Run time error --> null point exception
intilization:
int[] a = new int[6];
a[0]= 10;
a[1]= 20;
a[2]= 30;
a[3]= 40;
a[4]= 50;
a[5]= 60;
a[6]= 10; ==> RE: index out of bond
exception 😥😥😥
a[-5]= 100; ==> RE: 😥😥😥
a[2.8] = 15; ==> CE: found: Double,
required: int 😥😥😥
====================================================================
Array
declartion creation and intilization in a
single line
=====================================================================
1D array:
declartion: int[]
a;
creation: a = new
int[3];
installzation:
a[0]= 10;
a[1]= 20;
a[2]= 30;
--------------------
single line
-----------------
int[] a = {10,20,30};
---------------------------
2D array
---------------------------
int[][] a = {{10,20},{1,2,3}};
------------------
3D array
-------------
int[][][] a =
{{{10,20},{1,2,3}},{{10,20},{1,2,3}},{{10,20},{1,2,3}}}
Annonymous array:
main purpose only
one time use
creation:
new int[]
{10,20,30};
new int[3]
{10,20,30}; 😥😥😥 ==> CE
int[] x = new
int[] {10,20,30}; ----> it is not annonymous any more.
Comments
Post a Comment