Fallowing icon

Saturday, October 6, 2012

Java array



       We learned about the variables before. Variables are used to store a value. One variable can store only one value. When you want to store 10 values in same data type, you want to declare 10 variables for it. 

     Example- When you want to store 10 numbers in Integer data type, you want to declare the 10 Integer data type variables.
   
    int firstNo,secondNo,thirdNo,forthNo,fifthNo,sixthNo,seventhNo,eighthNo, ninthNo,tenthNo;

           Now you think when you want to store 100 data's in same data type, above code is not suitable for that, because we want to declare 100 variables for that. 

    In this reason Java was introduced an array for minimizing this nuisance.



Array    

    
The Array is a data structure, it can hold multiple values of the same data type. The array is allowed to store primitive and reference data types.





   Array have a fixed size. The array size (length) an array is established when the array is created. Array index start with zero and its end is changed on our needs.


   Structures of the array-

               DATATYPE NAME[];
               DATATYPE[] NAME;
               DATATYPE[] NAME1,NAME2,...;   (Both are same data type)

   Arrays are declared using enclosing square brackets. 
         
        
Example-  1) int value[];
          2) String[] name;
          3) byte[] fName,sName,tName;
          4) Text[] memoryAd;
          5) int max[],min;
             
                                         
     The brackets can be change either before name or after the name. You can declare the arrays in a single statement like that, int[] value1,value2; those arrays are in same data type. 


Creating array object-  

     Array object can be created in two methods, one is using new key word and another one is initializing the contents.  
     
     int value[]=new value[7];
    
         We declare the integer type array and give size is 7.

     String giveName[]={"Soro","Jack","Rose"};

         We declare the String type array and give values for Soro,Jack and Rose, array is automatically generated size is 3. 



Accessing array-  

     After the array is initialized, you can access this array using subscript expression ([]).

The structure- 

     ARRAYNAME [subscript]; 

     The array is initialized using an index. We give this index between the brackets. The index is start with zero. 


Example- 


        giveName[0];    //output="Soro"
  

     We have already declared the giveName array, We give valued Soro,Jack and Rose for it. Now we get giveName[0] position. its output is Soro. 



Change the array elements-

     First type array name and brackets, then give index witch you want to change in brackets. Finally type the new value and assign it.   

  
Example-

        giveName[2]="Boston";

     We give the word "Rose" for 3 positions to giveName array. 
Now we replace the word "Rose" to the word "Boston".


    
Example-    


     import java.util.Scanner;
           class Test{
                int getValue;                                               
               String names[];
               Scanner s=new Scanner(System.in);            //create Scanner object 

                void getUserInput(){                                                                                //declare the getUserInput method   
                      System.out.print("How many names do you want to add =");
                      getValue=s.nextInt();
                      names=new String[getValue];       
                 }

                 void accessRound(){                                                                     //declare the accessRound method
                       for(int i=0;i<names.length;i++){           
                       System.out.print("Enter the "+(i+1)+" name =");
                       names[i]=s.next();       
                       }
                  }

                  void print(){                                                                                           //declare the print method
                        for(int i=0;i<names.length;i++){
                        System.out.println((i+1)+" name is = "+names[i]);
                         }
                   }

                  public static void main(String[] args) {                             
                          Test a=new Test();                                                                     //create the Test class object
                          a.getUserInput();                                   
                          a.accessRound();
                          a.print();         
                  }   
         }


 





Discussion-

   void getUserInput(){} -
                  This method declares for getting user input to identify the array size. Then create an array object is using this value for array size.   


       void accessRound(){} -
                                                      This method is used for getting names to the array. We declare the for loop and give boolean Expression for i<names.length. names.length same to the names array size, It's passed an integer value.


    void print(){} -
             This method is used for print names array. We create for loop and check the variable i is less than to the names array length. Then declare the print statement and print array using for loop.    
          

        

  

No comments:

:a   :b   :c   :d   :e   :f   :g   :h   :i   :j   :k   :l   :m   :n   :o   :p   :q   :r   :s   :t

Post a Comment