7. ARRAY

Hi friends what’s up? I hope you are well. In last tutorial we learning about Boolean. Today we will learning about arrays.
An array is a group of like-typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more dimensions.
One-Dimensional Arrays
A one-dimensional array is, essentially a list of like-typed variables. To create an array, you first must create an array variable of the desire type. General form of one-dimensional array declaration is
         type var_name[];
For example, the following declares an array named month_days with the type “array of int”:
        int month_days[];
Although tis declaration establishes the fact that month_days is an array variable, no array actually exists. In fact, the value of month_days is set to null, which represents an array with no value. To link month_days with an actual, physical array of integers, you must allocate one using new and assign it to month_days. new is a special operator that allocate memory.
The general form of new as it applies to one-dimensional arrays appears as follows:
         array_var=new type[size];
This example allocates a 12-element array of integers and links them to month_days.
         month_days = new int[12];
Let’s go to complete example:
public class Array {
      public static void main(String args[]){
         int month_days[];
         month_days = new int[12];
         
         month_days[0] = 31;
         month_days[1] = 28;
         month_days[2] = 31;
         month_days[3] = 30;
         month_days[4] = 31;
         month_days[5] = 30;
         month_days[6] = 31;
         month_days[7] = 31;
         month_days[8] = 30;
         month_days[9] = 31;
         month_days[10] = 30;
         month_days[11] = 31;
         
         System.out.println("April has " + month_days[3] + " days.");
      }
}
I hope you now understand array. But still you do not see the advantage of array. Think we have to take ten input…….
import java.util.Scanner;
public class teninput {
      public static void main(String args[]){
         Scanner pavel = new Scanner(System.in);
         int mostafij[] = new int[10];
         int total = 0;
         
         for(int i=0;i<10;i++){
                System.out.println("Enter an ineger number ");
                mostafij[i] = pavel.nextInt();
                total = total+mostafij[i];
         }
         
         int avarage = total/10;
         System.out.println("Avarage of these number is " + avarage);
 
      }
}
Now I hope you are able to find the advantage of arrays. Advantage you will take many number and out many number in a easy way by using arrays.
Multidimensional Arrays
In java multidimensional arrays are actually arrays of arrays. These, as you might expect, look and act like regular multidimensional arrays. However, as you will see, there are a couple of subtle differences. To declare a multidimensional array variable, specify each additional index using another set of square brackets. For example, the following declares a two-dimensional array variable called twoD.
     int twoD[][] = new int[4][5];
This allocates a 4 by 5 array and assigns it to twoD. Internally this matrix is implemented as array of array of int.
Here a example:
public class towD {
      public static void main(String args[]){
         int TowD[][] = new int[4][5];
         int i,j,k=0;
         
         for(i=0;i<4;i++){
                for(j=0;j<5;j++){
                       TowD[i][j] = k;
                       k++;
                }
         }
         
         for(i=0;i<4;i++){
                for(j=0;j<5;j++){
                       System.out.printf(TowD[i][j] + "\t");
                }
                System.out.println();
         }
      }
}  
Output:
0      1      2      3      4     
5      6      7      8      9     
10     11     12     13     14    
15     16     17     18     19    
Ok here end now. See you in next tutorial. If you have any problem about this tutorial you can tell me I will trying my best to help you.

Penulis : Mostafij ~ Sebuah blog yang menyediakan berbagai macam informasi

Artikel 7. ARRAY ini dipublish oleh Mostafij pada hari Tuesday, April 9, 2013. Semoga artikel ini dapat bermanfaat.Terimakasih atas kunjungan Anda silahkan tinggalkan komentar.sudah ada 0 komentar: di postingan 7. ARRAY
 

0 comments:

Post a Comment