Thursday, June 21, 2018

Technical Tip #23 - Technical Tip of the day....


T23: Knowledge Tip Of The Day....
Arrays in Java
An array is a group of similar typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more dimensions. A specific element in an array is accessed by its index.
Defining and constructing one dimensional array::
$ Shows in first image
Type specifies the type of variables (int, boolean, char, float etc) being stored, size specifies the number of elements in the array, and arrayname is the variable name that is the reference to the array. Array size must be specified while creating an array.
Initializing array::
You can initialize specific element in the array by specifying its index within square brackets. All array indexes start at zero.
$ Shows in second image
resultArray[0]=69;
Multidimensional Arrays
To declare a multidimensional array variable, specify each additional index using another set of square brackets.
$ Shows in third image
int twoDim[][] = new int[2][3];
Initializing Two Dimensional array::
twoDim[0][0]=1;
twoDim[0][1]=2;
twoDim[0][2]=3;
twoDim[1][0]=4;
twoDim[1][1]=5;
twoDim[1][2]=6;
Important points:
- You will get “ArrayIndexOutOfBoundsException” if you try to access an array with an illegal index, that is with a negative number or with a number greater than or equal to its size.
- Arrays are widely used with loops (for loops, while loops). There will be more sample program of arrays with loops tutorial.





No comments:

Post a Comment