Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Array and it's type in java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 424
    Comment on it

    In this blog we will learn about array and it's type in Java. Array is defined as a collection of similar type of elements.

             Array are seen as a collection of elements these elements belongs to same data type and are assigned locations in memory in a sequence. In Java array is an object with similar data type. Array is a data structure where in we store elements of similar type (such as variables of similar type or objects of similar type). The index in the array starts at 0. Fixed number of elements can be stored in an array. Array can be one dimensional and multi-dimensional.


    One dimensional Array:

    Syntax:
    	array type  identifier[] = new array type[size of array];
              We use keyword new for creating an array.
    	For example: 
    	int myArray[] = new int[3];  // Here the array type is int and 
    	array name is myArray and 3 is the size of elements that we
    	can store in myArray.  

     

    The size of the array has to be specified while we are creating an array. In the above example we created an array of type int and size is 3.The size 3 will declare that the array can store only 3 values which can not be increased or decreased further.


    Array Literals Syntax:

    We can declare an array using indexes or literals. An absence of an object or an array is represented by the null literal.

    For example:
    int [] count = null;

    Another way of instantiating, initialization and array is as below. This syntax is used only when declaring a variable of array type. It combines the creation of the array object with the initialization of the array elements. It is called the array literal syntax:

    String[] monthsInYear = {Jan, Feb, Mar, Apr,May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

     

    In the example above we created an array consisting 12 elements of type string which represents months in a year within the curly braces. Instantiating an array in this way, we do not use keyword “new” or specific the type of the array it is done implicitly in the variable declaration. The size of an array is not specified explicitly, it is determined by the count of the elements between the curly braces.


    Initializing an array elements:

    To initialize the elements of an array we specify an index (all index for array in java starts from 0) within square brackets such as [0], [1].

    For example: 
    myArray[0] = 'Mussoorie'; // This initialized the first element of myArray[] with string 				'Mussoorie';
    myArray[1] = 'Dehradun'; // This initialized the first element of myArray[] with string 				'Dehradun';
    myArray[2] = 'Haridwar'; // This initialized the first element of myArray[] with string 				'Haridwar';

    Multidimensional Arrays:

    Multidimensional arrays are arrays of arrays. For declaring a multidimensional array variable we write an additional set of square brackets wherein we specify the index for two dimensional array and again another set of square brackets to specify index for three dimensional array.

    Example of two dimensional array:
    In this two dimensional array we create a matrix of size 2x4 in memory.
    int twoDimArray[][] = new int[2][4];
    Below are the elements in the above declared two dimensional array:
    twoDimArray[0][0], twoDimArray[0][1], twoDimArray[0][2], twoDimArray[0][3],
    twoDimArray[1][0], twoDimArray[1][1], twoDimArray[1][2], twoDimArray[1][3]
    Example:
    public class demoTwoDim{
    public static void main(String[] args){
    	int twoDimArray[][] = new int[2][4];
    	twoDimArray[0][0]  = '1'; 
    	twoDimArray[0][1]  = '2'; 
    	twoDimArray[0][2]  = '3';
    	twoDimArray[0][3]  = '4';
    	twoDimArray[1][0]  = '5'; 
    	twoDimArray[1][1]  = '6';
    	twoDimArray[1][2]  = '7';
    	 twoDimArray[1][3] = '8';
    
    System.out.println(twoDimArray[0][0]+   +twoDimArray[0][1]+   +twoDimArray[0][2]+   +twoDimArray[0][3]);
    System.out.println(twoDimArray[1][0]+   +twoDimArray[1][1]+   +twoDimArray[1][2]+   +twoDimArray[1][3]);
    }
    }
    Output:
    1 2 3 4
    5 6 7 8

    Arrays Manipulation:

    In Java we can import a helper class java.util.Arrays. This class is used for array manipulation. This method consists number of utility methods such as printing values of all array elements, sorting an array, searching array elements, dynamically changing the size of array etc.

    Note: On accessing an array with an illegal index (such as number greater than the size of array , negative number ) we receive an exception named “ArrayIndexOutOfBoundsException”.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: