Java Arrays

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. Each item in an array is called an element, and each element is accessed by its numerical index.




Types Of Array :
  • Single Dimensional Array
  • Multidimensional Array
Array Declaring :

   int[] anArray;

Creating an Array : 

  anArray = new int[10];  // new is an operator to create memory
 
Initializing an Array :

// initialize first element 
  anArray[0] = 10;
// initialize second element
  anArray[1] = 15;
...
...
// and last element
  anArray[9] = 200;

 Accessing an Array :


System.out.println("Element 1 at index 0: " + anArray[0]);
 
Output : 

 Element 1 at index 0:10
 

Example : 

   class ArrayDemo{
      public static void main(String args[]){ 
    
      //Another way we can declaring, creating and initializing an array
    
        int a[]={5,3,4,7};
    
           for(int i=0;i<a.length;i++){
                 System.out.println("Element at index "+i+": "+a[i]); 
            }
 
      }
} 
 
Element at index 0 : 5
Element at index 1 : 3
Element at index 2 : 4
Element at index 3 : 7 





Find us :
        Facebook : @apnaandroid
        Google+   : Apna Java
      Youtube :  Android & Java Tutorial

Comments

Popular posts from this blog

Disable/Hide Year from DatePickerDialog in android

Custom Calendar in android

View More and View Less in Android