Tuesday, 2 February 2016

Arrays

By

Lets take an Example where customer can have different contact numbers as residence number, mobile number, office number and create a class which can show us how they are declared.
class Customer{
                private int customerId;
                private long residenceNo;
                private long mobileNo;
                private long officeNo;
                public void setCustomerId(int id){
                                customerId = id;
                }
                public void setResidenceNo(long no1){
                                residenceNo=no1;
                }
                public void setMobileNo(long no2){
                                mobileNo=no2;
                }
                public void setOfficeNo(long no3){
                                officeNo=no3;
                }

}
Here we have three similar type of variable of type long is their a better way of storing this data?
Yes there is Arrays.
Arrays
  • An Array is a collection of similar type of data in contiguous locations of memory having same name.
  • Arrays can be used to store data as primitives type and reference type.
  • Array are created dynamically in java.
  • A new operator is used to dynamically allocation of memory to arrays
  • Arrays are of two type:
    • Single dimensional
    • Multi dimensional
Single Dimensional Arrays
 Array with one dimension (one row) is single dimensional array.

Way of creating and initializing :
Method 1: 
int array[] = new int[2];(Declaration)

array[0] = 1; (Initialization)
array[1] = 2;
Method 2: 

int array_Var[] = {1,2,3};(Declaration and Initialization)

The symbol [] can be placed before of after the variable i.e
int [] array_Var = new int[];
int array_Var[] = new int[]; (This is used for more readability)

  • Array variable is reference variable so stored in stack.
  • Arrays elements are dynamic and stored in heap.
  • The default value for array is the type of which it is declared in this case int so zero(0),if not explicitly initialized. 
Length property: Size f array can be defined using the length property of array and can be obtained using array_var.length which is 3 in this case.

int array_var[] = new int[5];
array_var[0] = 1;
array_var[0] = 2;
array_var[0] = 3;

Size will be array_var.length = 5

Multi Dimensional Arrays

Array can have multiple dimensions so are called Multi dimensional arrays 
Ways of declaring and initializing Multi dimensional arrays
Method 1:
char [][] multi_array = new char[2][2];(Declaration)
char[0][0] = 'a';
char[0][1] = 'b';
char[1][0] = 'c';
char[1][1] = 'd';
Method 2:
char[][] multi-array= {{'a','b'},{'c','d'}};
Length property: 
Number of row can be obtained by following statement
multi_array.length = 2
Number of columns in a row can be obtained by
array name[row_number].length
Like:
char[][] multi-array= {{'a','b'},{'c','d'}};
 multi_array[1].length = 2
char[][] multi-array= {{'a','b'},{'c'}};
 multi_array[1].length = 1
Now our Customer class will look like this 
class Customer{
   private int customerId;
   private long contactNos[] = new long[3];
   public void setCustomerId(int id){
     customerId = id;
   }
   public void setContactNo(long no1,long no2,long no3){
   contactNos[0]=no1;
   contactNos[1]=no2;
   contactNos[2]=no3;
  }
}
it is more efficient and with less line of code

0 comments :

Post a Comment