Arrays in Java
2. Array Declaration
3. Array Creation
4. Array Initialization
5. Array declaration,creation & initialization in single line
6. length vs length()
7. Anonymous Arrays
8. Array element assignment
9. Array variable assignment
INTRODUCTION
An array is an indexed collection of fixed no. of homogeneous data elements.
The main advantage of array is we can represent huge number of values by using single variable so that readability of code will be improved.
but the main disadvantage of array is fixed in size i.e, once we creates an array , there is no chance of increase or decrease of size based on our requirement.
Hence to use array we should know the size in advance, which may not possible always.
ARRAY DECLARATION
- ONE DIMENSIONAL ARRAY
int[] x; --> Recommended , because name is clearly separated from type
int []x;
int x[];
At the time of declaration we can't specify the size , otherwise we will get compile-time error.
int[6] x ; --> invalid
- TWO DIMENSIONAL ARRAY
int[][] arr;
int [][]arr;
int arr[][];
int[] arr[];
int[] []arr;
int []arr[];
# Which of the following are valid ?
int[] a,b; a - 1D b - 1D
int[] a[],b; a - 2D b - 1D
int[] a[],b[]; a - 2D b - 2D
int[] []a,b; a - 2D b - 2D
int[] []a,b[]; a - 2D b - 3D
int[] []a,[]b; -- Compile-time error
- If we want to specify dimension before the variable , that facility is applicable only for first variable in a declaration.
- If we are trying to apply for remaining variables, we will get compile time error
int[] []a,[]b,[]c; --invalid
Every array in java is an object only, hence we can create array by using new operator.
int[] arr = new int[3];
arr is reference variable.
arr.getClass().getName(); --> [I
For every data type corresponding classes are available but these are part of java language and are not available to the programmer level.
At the time of array creation we should specify the size otherwise we will get compile time error.
int[] arr = new int[]; // invalid
int[] arr = new int[6]; //valid
Memory structure and java representation of 2-D array
ARRAY CREATION
int[] arr = new int[3];
arr is reference variable.
arr.getClass().getName(); --> [I
For every data type corresponding classes are available but these are part of java language and are not available to the programmer level.
Array types
|
Corresponding class name
|
int[]
|
[I
|
int[][]
|
[[I
|
double[]
|
[D
|
boolean[]
|
[Z
|
char[]
|
[C
|
byte[]
|
[B
|
int[] arr = new int[]; // invalid
int[] arr = new int[6]; //valid
An array of 0 size perfectly valid in java.
int [] arr = new int[0]; //valid
If we are trying to specify array size with some -ve int value, then we will get runtime exception saying NegativeArraySizeException.
int[] arr = new int[-5]; // no compile time error
// RuntimeException: NegativeArraySizeException
To specify array size , the allowed data types are byte, short, char, int. If we try to specify any other type then we will get compile time error.
int[] arr = new int['a'];
byte b = 20;
int[] arr = new int[b];
short s = 90;
int[] arr = new int[s];
int [] arr = new int[10l]; // CE: Possible loss of precision found long required int
The maximum allowed array size in java is 2147483647 which is the maximum value of int data type.
int[] arr = new int[2147483647]; may occur outof memory Exception if sufficient memory not available
int[] arr = new int[2147483648]; CE: integer number too large
TWO DIMENSIONAL ARRAY
- In java two dimensional array not implemented by using Matrix style.
- Sun people followed array of arrays approach for multi dimensional array creation.
- The main advantage of this approach is memory utilization will be improved.
int[][] x = new int[2][];
x[0] = new int[2];
x[1] = new int[3];
Memory structure and java representation of 3-D array
int[][][] x = new int[2][][];
x[0] = new int[3][];
x[0][0] = new int[1];
x[0][1] = new int[2];
x[0][2] = new int[3];
x[1] = new int[2][2];

Comments
Post a Comment