Friday, November 25, 2016

types of array - Define Single Multi Dimensional Array

        Arrays use in Java

            Define Single Multi Dimensional Array types

Array is also a data type which store more than one value in single variable, when you want to create single variable with more than one values then array is a best for that purpose. In java array is an object which contain elements of similar type. In java array store a value on index based, first index starts from zero (0).types of array

Types of array in java

In java there are two types of array
  o   Single dimensional array
  o   Multi-dimensional array

Single Dimensional Array

Syntax:

  o   Declaration:datatype[] arrayName; or
  o   Declaration:datatype []arrayName; or
  o   Declaration:datatype arrayName[];
  o   Initialization: arrayName = new datatype[size];
  o   Declaration and Initialization: datatype[] arrayName = new datatype[size];
types of array

Example:

singalDeminsionalArray

Now as you notice in above example I used two array. First array I just declare and initialize then individual I assigned values and in Second array I declare, initialize and also assigned the value at a time. Now as I mention loops section that I will discuss for each loop in later. In above I used for each loop. For each loop is special for an array so that’s why it also called array loop.types of array

      1 .       Syntax of for each loop

                for (datatype name : array of same datatype){
                // body of for loop
}
For, foreach you have to use same data types which array have, if your array is an integer type then in for each loop you must declare integer datatype variable.types of array

       2.       Multi-Dimensional Array

Syntax
                         
o   Declaration:datatype[][] arrayname;
o   Declaration: datatype [][]arrayname;
o   Declaration: datatype arrayname[][];
o   Initialization:arrayname = new datatype[size][size];
o   Declaration and Initialization:datatype[][] arrayname = new datatype[size][size];
o   First bracket show rows and second bracket show columns
types of array
types of array - Define Single Multi Dimensional Array
types of array - Define Single Multi Dimensional Array



No comments:

Post a Comment