Array

 Array

  •  it is a group of variables of same data type which have a same name.
  •  it is used when more than one variable of same datatypes are used.

1.One dimensional Array

  • it is a collection of variables with one row and multiple columns or one columns and multiple row.

             Syntax :

                   Data type arrayname[];

             Example:

                   Int marks[];

  • Here marks is the array of integer.this array is created but not ready to use.

            Syntax:

              Array_name = new data_type[size];

           Example:

             Marks=new int[20];

  •  Both statements are combine as follow:
  •  Int marks[]=new int[20];
  •  We can give value as follow:
  • Marks[0]=55;
  • Int marks[]={5,6,7,8,9}

2.Rectangular array(two dimensional array)

  •  it will come in the tabular form in row or column.

     Syntax:

          Datatype arrayname[] [] = new datatype[row][column];

    Example:

          Int marks[][]=new int[5][5];

          Int marks[][]={{1,2,3},{4,5,6},{7,8,9}};

3.Jagged Array

  •  In jagged arrays, each row, in a two-dimensional array, may contain different length.
  •  In this we can not only create two dimensional array but in this we can put different number of column in different row.
  •  For example in matrix if we create 3 row then we can put 1 column in first row, 3 column in second row and 4 column in third row.

4.Command line argument array

  •  A java application can accept any number of arguments from the command line.
  • This allows the user to specify configuration information when the application is launched.
  •  The user enters command line arguments when invoking the application and specifies them after the name of the class to be run.
  •  When an application is launched the run time system passes the command line argument to the application main method via an array of strings.

     Example:

          Public class first

         {

                 public static void main(String s[])

               {

                      For(String s:args)

                     {

                                System.out.println(s);

                     }

              }

           }

  • Now run the program

  1. Java First hot cold 
  2. Hot 
  3. Cold

No comments:

Post a Comment