Wednesday, 12 June 2013

8th day of training

ARRAY

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays already, in the main method of the "Hello World!" application. This section discusses arrays in greater detail.
Illustration of an array as 10 boxes numbered 0 through 9; an index of 0 indicates the first element in the array
An array of 10 elements.

Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.
The following program, ArrayDemo, creates an array of integers, puts some values in the array, and prints each value to standard output.
class ArrayDemo {
    public static void main(String[] args) {
        // declares an array of integers
        int[] anArray;

        // allocates memory for 10 integers
        anArray = new int[10];
           
        // initialize first element
        anArray[0] = 100;
        // initialize second element
        anArray[1] = 200;
        // and so forth
        anArray[2] = 300;
        anArray[3] = 400;
        anArray[4] = 500;
        anArray[5] = 600;
        anArray[6] = 700;
        anArray[7] = 800;
        anArray[8] = 900;
        anArray[9] = 1000;

        System.out.println("Element at index 0: "
                           + anArray[0]);
        System.out.println("Element at index 1: "
                           + anArray[1]);
        System.out.println("Element at index 2: "
                           + anArray[2]);
        System.out.println("Element at index 3: "
                           + anArray[3]);
        System.out.println("Element at index 4: "
                           + anArray[4]);
        System.out.println("Element at index 5: "
                           + anArray[5]);
        System.out.println("Element at index 6: "
                           + anArray[6]);
        System.out.println("Element at index 7: "
                           + anArray[7]);
        System.out.println("Element at index 8: "
                           + anArray[8]);
        System.out.println("Element at index 9: "
                           + anArray[9]);
    }
} 
The output from this program is:
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000
Create an array in java to find greatest
 number
  1. import java.io.*;  
  2.    
  3. public class Accept{  
  4.   public static void main(String[] args) throws IOException{  
  5.   String str;  
  6.   int value[]=new int[10];  
  7.   for(int a=0;a<10;a++){  
  8. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  
  9. System.out.println("Enter Number : ");  
  10. str=br.readLine();  
  11. value=Integer.parseInt(str);  
  12. }  
  13. System.out.println(compare());  
  14. }  
  15.    
  16. public static Integer compare(){  
  17.   int largest=new int[10];  
  18.   For(int k=0;k<10;k++){  
  19.        if (value[k] > largest )  
  20.           largest=value[k];  
  21.   }     
  22. return largest;           
  23. }  
  24. }   

No comments:

Post a Comment