Wikipedia

Search results

Monday, 21 October 2013

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. 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.



Arrays are basically memory location ,it will store the multiple Data,it has the index,

*Some drawbacks of Array 
1)It is fot particular DataTypes
2)Size of the Array

int[i]=new int[10]//Advantage of the Array is Length of the Array

 Object Array :Object array is a inbuilt class it is super class of the class
It can Store Different type of DataTypes
Object ar[] = new Object[4];
ar[0] = "Hello"
ar[1]="1231" 
ar[2]="true"
ar[3]="32.21"

Simple Programm for Array

public class TestArray {

   public static void main(String[] args) {
      double[] myList = {1.9, 2.9, 3.4, 3.5};

      // Print all the array elements
      for (int i = 0; i < myList.length; i++) {
         System.out.println(myList[i] + " ");
      }
      // Summing all elements
      double total = 0;
      for (int i = 0; i < myList.length; i++) {
         total += myList[i];
      }
      System.out.println("Total is " + total);
      // Finding the largest element
      double max = myList[0];
      for (int i = 1; i < myList.length; i++) {
         if (myList[i] > max) max = myList[i];
      }
      System.out.println("Max is " + max);
   }
}



No comments:

Post a Comment