ARRAY – Seminar – 1.1.0. in Java – Payilagam – Part – III .

This blog  deals with  ARRAY – Seminar – 1.1.0. in Java – Payilagam – Part – III . Some of the illustrations are discussed below.  

   In the previous blogs ARRAY – Seminar – 1.1.0. in Java – Payilagam – Part – I and Part II  we have already explained about some finer examples . Again in the upcoming blogs we are dealing with the Array – 1.1.0 . They are mentioned here in ARRAY – Seminar – 1.1.0. in Java – Payilagam – Part – III  below as follows 

REFERENCE : ( For ARRAY – Seminar – 1.1.0. in Java – Payilagam – Part – I  & Part – II )

https://payilagam.com/wp-admin/post.php?post=15819&action=edit

https://payilagam.com/wp-admin/post.php?post=15826&action=edit

Payilagam Trainer  : Ramaiah Arunachalam                                            Version :      1.1.0

Prepared By              : Ravikumar Sivaramakrishnan                              Date       :       Sep-23-2018.

_______________________________________________________________________________

                                            Default Values Of Array Types

If we don’t assign values to array elements, and try to access them, compiler does not produce error as in case of simple variables. Instead it assigns values which aren’t garbage.

Below are the default assigned values.

boolean : false

 int : 0

double : 0.0

 Object : null

String : null

User Defined Type : null

 public class DefaultValuesOfArrayElements {

 public static void main(String[] args) {

 // declares an array of integers

 //int[] anArray=new int[5];

 //Object[] anArray=new Object[5];

 boolean[] anArray=new boolean[5];                                                                                                             

// Accessing elements without initialization for (int i=0;i<anArray.length;i++)

{ System.out.println(“Element at index “+ i+”: ” + anArray[i]);

} System.out.println(“Length of anArray is “+anArray.length);

}

}

Output

+++++++++++++++++++++++++++++++++++++++++++++

              Array Types Based on Dimension

1. One dimensional Array

2.Multi dimensional Array 

                                          Multi dimensional Array

Multi-dimensional arrays are simply nested arrays. A two-dimensional array is an array of arrays. A three-dimensional array is an array of two-dimensional arrays, and so on.

Syntax: 1.Declaring Array type[ ] [ ]

arrayname = new type[size] [size] ;

Hint-àRowC Int [ ] [ ] myarray = new int[2] [2] ;

2. Initializing Array

arrayname[index] [index] = value;

// Late initialization class MultiDimArrayDemo

{ public static void main(String[] args) {

// Two dimensional String array is declared String[][] names = new String[2][2];

// Late initialization of Two dimensional String array. names[0][0] = “Mr. “;

names[0][1] = “Ms. “; names[1][0] = “Smith “;

names[1][1] = “Jones “;

System.out.println(names[0][0] + names[1][0]);

// Mr. Smith System.out.println(names[0][1] + names[1][1]);

// Ms. Jones

}

} The output of above program .

3. Creating Array with initialization

Syntax:

type[ ] [ ] arrayname ={ {value1,value2,….,value n},{value1,value2,….,value n}};

type can be primitive type or Object class or sub class of Object class or user defined class. class MultiDimArrayDemo { public static void main(String[] args)

{ String[][] names = { {“Mr. “, “Mrs. “, “Ms. “}, {“Smith”, “Jones”} };

// Mr. Smith System.out.println(names[0][0] + names[1][0]);

// Ms. Jones System.out.println(names[0][2] + name>

System.out.println(“Element at index “+ i+”: ” + anArray[i]);

} System.out.println(“Length of anArray is “+anArray.length);

 

______________________________________________________________________________