Thursday, December 4, 2014

We are of same type, we stay together...

Sometimes in life we deal with single elements, priority goes to unified form of mass...

That's true. In real life we have to deal with things and not only a single piece of any object. We have to collect things, we have number of students in a class, we have number of classes in a school even we have number of people in our family. Human race may differ in nature from person to person but it is true that they stay together. So, do objects in the world of Java.

In Java, we can define a container which can store multiple number of similar type objects.

I got the container concept. But what do you mean by similar type ?
Well, this part is important to know about things, but first let's go through a formal definition set.

Array: Array is a container object that holds multiple number of values of similar types. Arrays can hold primitives and object references.
Length of array: Array can have more than one similar type element. The number of elements an array holds is known as the length of the array. Arrays need to know about the length at the time of definition. So, it can make places for that much element in the memory.

Array can be represented as the following,
Array in memory

These are the basic details we need to know before we go into further detail. Now, lets put altogether in code.

 /**  
  *   
  * @author palash.k  
  *   
  */  
 public class Main {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           // First form of array definition  
           short[] shortArr = new short[25];  
           long[] longArr = new long[25];  
           byte[] byteArr = new byte[25];  
           int[] intArr = new int[25];  
           char[] charArr = new char[25];  
           float[] floatArr = new float[25];  
           double[] doubleArr = new double[25];  
           boolean[] boolArr = new boolean[25];  
           String[] stringArr = new String[25];  
           // Second form of array definition  
           short shortArray[] = new short[25];  
           long longArray[] = new long[25];  
           byte byteArray[] = new byte[25];  
           int intArray[] = new int[25];  
           char charArray[] = new char[25];  
           float floatArray[] = new float[25];  
           double doubleArray[] = new double[25];  
           boolean boolArray[] = new boolean[25];  
           String stringArray[] = new String[25];  
      }  
 }  

The definition of an array has two forms. First we need to define the data type of e elements the array holds, the braces signify that the variable is an array. Well, the braces can be placed in either form as shown above. Then we need to initialize the array with the new keyword, otherwise, we can not use the array. Within the braces in the right side, we put the length of the array. So, in our case all the arrays have the length of 25.
From the above code, we can tell that. floatArray will hold 25 float elements in it.

OK, I got it. Array is a container of similar type of element. but what's the use ?
Yes, that's a major point, what's the use of an array. Array can be used in many ways. Specially for matrix manipulation, arrays are useful. But before going to that part, we need to know more about arrays.

Next, we are going to check for accessing the elements.
Index: Array elements are defined stored in the memory with a number associated with it. This number is known as index. To access an array element we need to know the index of the array. So, providing the index, we can get the element at the index.
Index of an array starts from 0 and it ranges upto length-1;
For example, an array with length 25 will have index 0 - 24.
So, to access the first element we need to use index 0, to access second element, we need index 1 and so on...

Let's check this in our code...

 /**  
  * Array test  
  *   
  * @author palash.k  
  *   
  */  
 public class Main {  
      public static void main(String args[]) {  
           // First form of array definition  
           short[] shortArr = new short[25];  
           long[] longArr = new long[25];  
           byte[] byteArr = new byte[25];  
           int[] intArr = new int[25];  
           char[] charArr = new char[25];  
           float[] floatArr = new float[25];  
           double[] doubleArr = new double[25];  
           boolean[] boolArr = new boolean[25];  
           String[] stringArr = new String[25];  
           // We defined arrays, let's put some values  
           // Assigning values to array elements  
           shortArr[0] = 0;  
           shortArr[1] = 1;  
           longArr[0] = 2;  
           longArr[1] = 3;  
           byteArr[0] = 4;  
           byteArr[1] = 5;  
           intArr[0] = 6;  
           intArr[1] = 7;  
           charArr[0] = 'a';  
           charArr[1] = 'b';  
           floatArr[0] = 1.0f;  
           floatArr[2] = 2.14f;  
           doubleArr[0] = 2.54;  
           doubleArr[1] = 3.14;  
           boolArr[0] = true;  
           boolArr[1] = false;  
           stringArr[0] = "Palash";  
           stringArr[1] = "Kanti";  
           stringArr[2] = "Kundu";  
           // Accessing array elements  
           System.out.println(shortArr[0]);  
           System.out.println(shortArr[1]);  
           System.out.println(longArr[0]);  
           System.out.println(longArr[1]);  
           System.out.println(byteArr[0]);  
           System.out.println(byteArr[1]);  
           System.out.println(intArr[0]);  
           System.out.println(intArr[1]);  
           System.out.println(charArr[0]);  
           System.out.println(charArr[1]);  
           System.out.println("Addition of bothe float elements gives us: "  
                     + floatArr[0] + floatArr[1]);  
           System.out.println("Addition of bothe double elements gives us: "  
                     + doubleArr[0] + doubleArr[1]);  
           System.out.println(boolArr[0]);  
           System.out.println(boolArr[1]);  
           System.out.println(stringArr[0] + " " + stringArr[1] + " "  
                     + stringArr[2]);  
      }  
 }  
The output of the program is:

 0  
 1  
 2  
 3  
 4  
 5  
 6  
 7  
 a  
 b  
 Addition of bothe float elements gives us: 1.00.0  
 Addition of bothe double elements gives us: 2.543.14  
 true  
 false  
 Palash Kanti Kundu  


This is how we access array elements. But with looping construct, we can have a finer control over arrays,

 /**  
  * Array test  
  *   
  * @author palash.k  
  *   
  */  
 public class Main {  
      public static void main(String args[]) {  
           // First form of array definition  
           int[] intArr = new int[25];  
           int i = 0;  
           // Assign array elements using loops  
           while (i < 25) {  
                intArr[i] = i;  
           }  
           // Access array elements using loops  
           for (i = 0; i < 25; i++) {  
                System.out.println(i);  
           }  
      }  
 }  

Arrays are useful in matrix calculations. To go into that part, we'll have to take a look in multi-dimensional arrays. Which we'll look in our later discussions.

Prev     Next
Palash Kanti Kundu

3 comments:

  1. Replies
    1. Here is the next part http://java.palash90.in/2014/12/multi-dimensional-array.html

      Delete