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

This blog  deals with ARRAY – Seminar – 1.1.0.  Some of the illustrations are discussed below.  

   In the previous blog ARRAY – Seminar – 1.1.0 in Java – Payilagam – Part – I 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 below as follows :  

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


                    Declaring a Variable to Refer to an Array

The preceding program declares an array (named anArray) with the following line of code:

// declares an array of integers

int[] anArray;

Like declarations for variables of other types, an array declaration has two components: the array’s type and the array’s name. An array’s type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array. As with variables of other types, the declaration does not actually create an array; it simply tells the compiler that this variable will hold an array of the specified type.

Similarly, you can declare arrays of other types

byte[] anArrayOfBytes;

short[] anArrayOfShorts;

long[] anArrayOfLongs;

float[] anArrayOfFloats;

double[] anArrayOfDoubles;

boolean[] anArrayOfBooleans;

char[] anArrayOfChars;

String[] anArrayOfStrings;

You can also place the brackets after the array’s name:

// this form is discouraged float anArrayOfFloats[];

However, convention discourages this form; the brackets identify the array type and should appear with the type designation.

         Creating, Initializing, and Accessing an Array

One way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for 10 integer elements and assigns the array to the an Array variable

// create an array of integers

int[] anArray = new int[10];

If this statement is missing, then the compiler prints an error like the following, and compilation fails: Variable anArray may not have been initialized.

The next few lines assign values to each element of the array:

anArray[0] = 100; // initialize first element

anArray[1] = 200; // initialize second element

anArray[2] = 300; // and so forth

Each array element is accessed by its numerical index:

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

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

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

Alternatively, you can use the shortcut syntax to create and initialize an array:

int[] anArray = {

            100, 200, 300,

           400, 500, 600,

           700, 800, 900, 1000


Here the length of the array is determined by the number of values provided between braces and separated by commas.