Getting familiar with Java: Arrays

In this chapter we will study about ‘Arrays’. First the similar process of creating a class namely ‘ExampleArrays’ with Eclipse. Go to FILE and then NEW and then CLASS. Then it will open another window namely, NEW JAVA CLASS. In the space of NAME write down ‘ExampleArrays’ and in the space of ‘Which method stubs would you like to create’ tick on ‘public static void main’ and then click on FINISH to create the ‘ExampleArrays.java’  file in the class of ‘ExampleArrays’. This will be the autogenerated methods in this class and it will be as follows.

package day1.examples;



public class ExampleArrays {



       public static void main(String[] args) {

              // TODO Auto-generated method stub



       }



}

 

Here, in the package or folder of java installation of day1.examples, we have public class namely, ExampleArryas and then the method the public static void main and then the two closing parenthesis to follow after two opening parenthesis.

What is Arrays:

According to Java docs, “an array is a container object that holds fixed number of values of a single type”. Each item of an array is called as element. Each element is a numerical index. Arrays is an efficient way of storing large amount of uniform and efficient data. In other words an array is an efficient way of storing a bunch of homogenous data.

First way to provide the solid data information or arrays as follows.

package day1.examples;



public class ExampleArrays {



       public static void main(String[] args) {

              // TODO Auto-generated method stub



        /* int a = 10;

         int b = 20;

         int c, d, e;

         c = 30;

         d = 40;

         e = 50;*/

             

       /*  int a1 = 10;

         int a2 = 20;

         int a3 = 30;

         int a4 = 40;

         int a5 = 50;*/

             

       }



}

 

Here, we have two sets of block comment segment. These two are two different ways to put particular value to variables which are homogenous in nature. But these two are not so efficient way to do this as we see that all of these are long and could provide enough ways to confuse the user and can make the code prolonged. These two commented segment are not so efficient way to put arrays.

What is an efficient way of storing large amount of uniform data (Arrays)?

If you want to store large amount of uniform data which is called as array. It is like as follows.

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

It is like integer and then square bracket and then equals to all the numbers that are inside parenthesis and all of these numbers are homogenous in character. So, here we declare array and this means that java will create boxes for array. Here, we declared that a is an array which has five numbers that is {10, 20, 30, 40, 50}; and the square bracket [] is the sign of presence of array and it is situated before a and that means that the name of array is a and its values are inside parenthesis.

Name of array always begins with 0 and this means due to presence of five values which are of course homogenous in nature, and these values are stored inside boxes such as a[0], a[1], a[2], a[3], a[4]. Always remember that the value of arrays always begin with 0.

Java creates similar table of array a and it has five values which means five tables for it. The values of array a which are inside parenthesis are distributed here. These are five boxes and the label of boxes are as a[0], a[1], a[2], a[3], a[4] and it is called as a of 0 and then a of 1 and then a of 2 and then a of 3 and then a of 4. Then it will put those five numbers in these boxes such as above.

How will you access these data in the array?

In the above example if you want to access and print out some data of array then you suppose want to print out a[2] and a[4]. It is simple just write down the a[2] and a[4] in the print function. The code will be like this.

System.out.println(a[2] + ” ” + a[4]);

First we have system.out.println which is a command to complie to print out something and then we have a of 2 which is 30 in value and then we have a of 4 which is 50 in value and in between we have + “ “ + and this means the space between two characters. Always remember that array always begins with 0 and the a of 0 is 10 and then a of 1 is 20 and a of 2 is 30, which will be printed and then a of 3 is 40 and a of 4 is 50 and this is the second number to be printed. So out put will be as follows.

30 50

 

Watch out of the space between 30 and 50 as we have put the space character between them. As we have commanded in println function as a of 2 which is 30 and a of 4 which is 50.

So the code is as follows:

package day1.examples;



public class ExampleArrays {



       public static void main(String[] args) {

              // TODO Auto-generated method stub



        /* int a = 10;

         int b = 20;

         int c, d, e;

         c = 30;

         d = 40;

         e = 50;*/

             

       /*  int a1 = 10;

         int a2 = 20;

         int a3 = 30;

         int a4 = 40;

         int a5 = 50;*/

             

              //Array delclaration: an efficient way of storing a bunch of homogenous data

             

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

        

         // space between a of 2 and a of 4, array element alwats begins with 0

        

         System.out.println(a[2] + " " + a[4]);

             

       }



}

As I have explained entire code so it should be easier to understand what is this code about and write this and run the Eclipse compiler function to see the result of compilation.

How to add block comment:

In Eclipse there is a short cut to insert block comment to specific set up statement and when you want to add comment to more that one sentence then it is called as block comment and for this select the portion of the comment to be made and then go to SOURCE of ECLIPSE  and then click ADD BLOCK COMMENT while selecting the portion to be made for block comment and it is done, so if you want to put this block comment in certain segment then you can do this way.

Leave a Reply

Your email address will not be published. Required fields are marked *

You are here

  • Home
  • Java
  • Getting familiar with Java: Arrays