Wednesday, 15 February 2012

An Approach

History:


In earlier days when the concept of computer programming was introduced to the world, coding with little variables were possible but when it came to very large amount of variables to solve complex problems like Sudoku and others, the programmers had to face a great deal of difficulties as they had to declare hundreds of variables separately which was a bit of hectic work to manage. This limited the scope of programming  and their implementation in several fields that awaited human exploration as it needed an efficient and organized way of doing so. 

In 1962, it was a Canadian computer scientist  Kenneth Eugene Iverson who introduced the concept of arrays to world. He showed the world how to handle large amount of variables in code. Since then arrays had been a favourite of programmers. Of course there had been some innovations in it as per the requirements of time. 

Now coming to the point, my purpose of writing this blog is to help you understand the concepts of arrays in java. Since I have been facing problems in handling array operations in java so I started to figure it out on my own .
Now shall we start off??

Introduction:

The word Array simply means to place something in an orderly fashion, but the limitation is that they should be of same type. In programming language, it is a datastructure that holds a collection of same data type objects, as simple as that.
Suppose, we want to perform some operation on a set of 100 variables, we generally use array for that. Instead of taking variables form var0,var1,...,var99 we declare an array variable as var[100] where each entity var[0],var[1],...,var[99] represents individual variables.

Declaring Array Variables :

In order to use arrays in our program we must declare a variable first, in order to reference the array. And, its datatype must be the same as the variables we are storing in the array.
Syntax :
            datatype[] refVariable;
The index ,[], holds the arraySize as defined by user.
For example, int[100] array; defines an array of 100 variables of integer type.
[N:B - We can also declare it as datatype refVariable[]; but this isn't preffered in Java.]
    Memory Allocation : Unlike the primitive datatypes, simply the declaration of array variables doesn't allocate it memory but only creates a location to reference the array.
To create an array we do :
               datatype[] refVar;
               refVar=new datatype[arraySize];
[Here onwards we will use refVar to represent the array.]

Above code performs 2 operations: a) declares & creates an array using "new" operator;
b)assigns reference of newly created array to refVar.
The declaration,creation and reference assignment can be combined in one statement as datatype[] refVar=new datatype[arraySize].

Initialization :

The initialization can be done in two ways :
i)we assign values to each array item separately after declaration as :
     refVar[0]=5;
     refVar[1]=90;
     refVar[2]=100; and so on. Or,
ii)we can initialize the array during declaration only as:
     int refVar[]={5,90,100,..}; this assigns the values to array variables from         0 to arraySize-1 in order as specified in the brackets.

[N:B - The "new" operator is not used in array initializer syntax because
  int refVar[]={5,90,100,..}; is equivalent to 
  int[] refVar=new int[arraySize];
  refVar[0]=5;
  refVar[1]=90;
  refVar[2]=100;
  ...]

We can find the length of array using the syntax:
   refVar.length, where refVar is the name of our array.
Once the array is created its elements are assigned default values,i.e, '0' for numeric primitive datatypes and '\u0000' for char types and false for boolean types.

Accesing and processing arrays :

The array items/elements are accessed through its index values, starting from 0 to refVar.length-1 and so they are also called as indexed variables.
An indexed variable is treated just like normal variables and so we can perform various operations on it.
For example, refVar[0]=refVar[1]+refVar[2];would make the value of refVar[0] as 190 (see initialization section for values).

In order to process the array or to perform some operation on all elements of array we oftenly use a 'for' loop.There are two obvious reasons for this:
a)since we know the size of array, so its obvious to use for-loop.
b)all elements are of same datatype so we can process them using a for-loop.

Some examples:
i) Printing elements:
  for(i=0;i<refVar.length;i++)
    System.out.print(refVar[i] + "\t");
ii)Sum of all elements:
   int toTal=0;
   for(i=0;i<refVar-1;i++)
      toTal+=refVar[i];
  System.out.println(toTal);

Jdk 1.5 introduced a new kind of for-loop called as for-each loop that enables to traverse the complete array sequentially without using an index variable.
  Syntax:
      for(dataType element:refVar){
        //Do sequential Processing.
      }
But, if we want to process in non-sequential order or each element separately,we still have to use indexed method.

In the next blog we will discuss topics like copying arrays, passing arrays as arguments, arrays as return types.

Thank You.





No comments:

Post a Comment