Saturday, May 21, 2016

Array

Array is a collection of same type variables referred by the same name. Each element of array can be accessed via index. In Java, array index always integer starting from 0. Here is a picture to illustrate an array:

  A[0]   A[1]   A[2]   A[3]   A[4]
  10   20   30   40   50

The above picture shows A array which contains five elements of integer type: 10, 20, 30, 40, and 50. Each element of the array can be accessed by A[index].


One-Dimensional Array


Arrays are declared with a [ ] (bracket). In Java, a common form of one-dimensional array declaration as follows:

type arrayName[];

or it can be written as follows:

type[] arrayName;

In this blog, we will use second form. type above signifies base type of each array element that exists. The base type will determine what type of data to be stored in each array element.

For example, if we want to know the number of days of each month, then we can implement it through an array. For example, here we are going to name the array with numberOfDays and data type of each element is int, then we can write it as follows:

int[] numberOfDays;

We just declared a variable with array type. The number of elements and values of each element is set to null value. To determine the number of elements of the array, we need to allocate memory space by using the new keyword. The new keyword itself will be explained more detail in next post. We need to use new keyword to determine the number of array elements. Here is the general form of use

arrayVariable = new type[numberOfElements];

In this case, we will determine the number of elements of the array with 12 (which contained a number of months in a year) so we need to write the following code:

numberOfDays = new int[12];

numberOfDays variable will refer to an array with 12 elements in int type, and the value of each element will be set to a 0. In order to fill value in each array element, we must use index, such as the following :

numberOfDays[0] = 31;

The above code will put value 31 into the first array element (in this case means that January has 31 days). Remember, array index start at 0, not 1.

Here is an example of code that demonstrates the declaration and use of arrays in Java.

class DemoArray1D {

   public static void main(String[] args) {

      //declare a variable with array type in int 
      int[] numberOfDays;

      //define number of array element
      numberOfDays = new int [12];

      //fill value in each array element
      numberOfDays[0] = 31;
      numberOfDays[1] = 28;
      numberOfDays[2] = 31;
      numberOfDays[3] = 30;
      numberOfDays[4] = 31;
      numberOfDays[5] = 30;
      numberOfDays[6] = 31;
      numberOfDays[7] = 31;
      numberOfDays[8] = 30;
      numberOfDays[9] = 31;
      numberOfDays[10] = 30;
      numberOfDays[11] = 31;

      //display an array element
      System.out.println("March has “ + numberOfDays[2] + " days.");
   }
}

The results will be given by the above program as follows:

March has 31 days.

Usually, Java professionals generally combine array declaration with the determination of the number of elements, as shown in the following code:

int[] numberOfDays = new int[12];

or

int numberOfDays[] = new int[12];

Array can also be initialized when declared. The values for each element are in the block that begins with { and ends with }, and each element is limited by a comma (,). After the initialization block, we have to add a semicolon (;). For more details, see the following code sample which is a modification of the previous program. In this example, the value of each element of the array will be initialized in the declaration.

class Array1DInitialization {

   public static void main(String[] args) {

      //declare a variable with array type in int and fill value in each array element
      int[] numberOfDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

      System.out.println("March has “ + numberOfDays[2] + “ days.”);
   }
}

The result provided by this program will be the same with the result given by the previous program.


Multi-Dimensional Arrays


Multi-dimensional array actually is array of array. There is an array where each element is also an array. The general way in Java to declare two-dimensional array as follows:

type arrayName[][];

or

type[][] arrayName;

For three-dimensional array, its general form as follows:

type arrayName[][][];

or

type[][][] arrayName;

Here is an example of two-dimensional array declaration with the number of lines 2 and the number of columns 3, which each element of type is int.

int[][] twoD = new int[2][3];

If described the position of each element as follows:

  twoD[0][0]   twoD[0][1]   twoD[0][2]
  twoD[1][0]   twoD[1][1]   twoD[1][2]

We can also do initialize the value of two-dimensional array through the following ways:

int[][] twoD = { {10,20,30}, {40,50,60} };

2 comments:

  1. Thank you for sharing valuable information with us.

    Java web application is a type of free programming language used to develop applications and software that are becoming highly in demand on the internet. The software engineer can provide the web application or software in the form of windows, Linux, and MAC OS. There are various types of systems that can not work without java and it is getting necessary day by day even the world’s no.1 application depends on java. Java has become a most popular feature in android smartphone applications that's why Aptech learning provides java training in Dwarka, Janakpuri, and in Gurgaon also. Java training helps to get you complete knowledge about web frameworks, Java testing tools, and design patterns in java.

    Let's get learn about the PHP training course
    PHP is a type of programming language that is most commonly used in dynamic functions such as modifying, collecting, and deleting all types of databases It also helps to encrypt all types of data in the form of HTML and XML file. Aptech learning provides you with a complete PHP training course at a reasonable cost that will help you to grow more in the era of the software industry.

    For more details please visit our website : Aptech Janakpuri

    ReplyDelete