Tuesday, 1 March 2016

Arrays


In this post we will discuss on

         Arrays
         Advantages and Disadvantages of Arrays

Arrays:

An array is a collection of similar data types. Arrays are fixed in size.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.   

Program using arrays

   
using System;
class Arrays {
 public static void Main() {

  // Initialize and assign values in different lines
  int[] EvenNumbers = new int[3];
  EvenNumbers[0] = 0;
  EvenNumbers[1] = 2;
  EvenNumbers[2] = 4;

  // Initialize and assign values in the same line
  int[] OddNumbers = { 1, 3, 5  };

  Console.WriteLine("Printing EVEN Numbers");

  // Retrieve and print even numbers from the array
  for (int i = 0; i < EvenNumbers.Length; i++) {
   Console.WriteLine(EvenNumbers[i]);
  }

  Console.WriteLine("Printing ODD Numbers");

  /* Retrieve and print odd numbers from the array
                   odd numbers from the array */
                   
  for (int i = 0; i < OddNumbers.Length; i++) {

   Console.WriteLine(OddNumbers[i]);
  }

 }
}
Output:
Arrays
Advantages: Arrays are strongly typed.

Disadvantages: Arrays cannot grow in size once initialized. Have to rely on integral indices to store or retrieve items from the array.

0 comments:

Post a Comment