Ad Code

Responsive Advertisement

ARRAYS

 REFERENCE DATA TYPE

Reference data type is classified into three categories: array, class and interface.

Difference between primitive and reference data type

·      For primitive data type like int, char, float, etc… size is defined by the compiler whereas in reference data type, size is defined by the user. ‘New’ keyword should be used with reference data type.

Array

For using arrays in Java, standard library file has to be imported before defining a class.

        import java.util.Arrays;

To store a single number, we can use int, byte, long or short data type. But when we need to store a set of numbers, array data type is used. More numbers can also be stored using int, byte, long or short, but the space and time consumed is comparatively more than an array. The syntax to create an array is:

        int [] array=new int[11];

Here, ‘int’ is the data type used since I am going to define an integer array. The square bracket ‘[]’ is an indication to create an array. ‘array’ is the variable name to which the integer is assigned. For all reference data type, ‘new’ keyword should be used. In int[11], ‘11’ represents the size of the array which depends on the user. Here the maximum size of the array is 11 and so if the array is assigned with more than 11 numbers, it results in compiler error. The square bracket can also be used after defining the variable name.

        int array[]=new int[11];

 Defining numbers in an array

       array [0]=11;

 Here ‘array’ denotes the array name (defined previously) and ‘array[0]’ represents the 0th position in an array. The 0th position is assigned with a number 11. By default, once an array is defined all the numbers are initialized to 0.


A quite easier method to define the number in the array is,

        int [] array=new int []{11,13,17,19};

Here the values are defined directly whereas in earlier case, the values are assigned separately.

Syntax to print an integer array

    System.out.println(Arrays.toString(array));

 ‘System.out.println’ is used to print the value and the format ‘Arrays.toString(array name)’ has to be used to print the values.

Java code to print an integer array

        import java.util.Arrays;
        class reference{
            public static void main(String args[]){
            int [] array=new int[11]; //defining an integer array
            //assigning values to integer array
            array [0]=17;
            array [1]=1;
            array [2]=7;
            array [3]=19;
            array [4]=20;
            //printing out the integer array
            System.out.println(Arrays.toString(array));
                }
            }

 

Output


In the code only 5 values are defined and so the remaining values are assigned 0.

Defining Multi-dimensional arrays in Java

For multi-dimensional arrays, based on the number of dimensions, square bracket is increased. For a 2D array, 2 square brackets are used.

        int array_2d[][]={{1,4},{1,7}};

                                                                                                                                        NEXT

Post a Comment

0 Comments