Saturday 8 October 2011

Array List

Question: Describe the Array List ?
Question : What is difference between Array and Arraylist.
Ans: Below full Description of Array list
Firstly i want to describe why use array list

 The main problem of simple array is that their size is fixed by the number you specify when declaring the array variable: you cannot add items beyond the specified dimension. Another limitation is that you cannot insert an item inside the list. To overcome this, you can create a linked list. Instead of working from this, the .NET Framework provides the ArrayList class. With the ArrayList class, you can add new items to a list, insert items inside a list, arrange items of a list, check the existence of an item in a list, remove an item from the list, inquire about the list, or destroy the list. These operations are possible through various properties and methods.
The ArrayList class is defined in the System.Collections namespace. To use it, first declare a pointer to ArrayList
Simple definition of Array list is 
An ArrayList is an array that can dynamically grow and shrink.
Method In Array list 
Add Method :
using System.Collections;
class Program
{
    static void Main()
    {
 // Create an ArrayList and add four element elements.
 ArrayList list = new ArrayList();
 list.Add("One");
 list.Add("Two");
 list.Add("Three");
list.Add("Four")
}
}
these program add the four elemnet in Arraylist 
Ex:Call the method in which parameter of this method is Array list
using System;
using System.Collections;
class Program
{
    static void Main()
    {
 // Create an ArrayList and add three method 
 ArrayList list = new ArrayList();
 list.Add(5);
 list.Add(7);
list.Add(11);
 // Use ArrayList with method.
 Example(list);
    }
    static void Example(ArrayList list)
    {
 foreach (int i in list)
 {
     Console.WriteLine(i);
 }
    }
}
=========

Output
5
7
11
Add one ArrayList to second one
using System;
using System.Collections;
class Program
{
    static void Main()
    {
 // Create an ArrayList with two values.
 ArrayList list = new ArrayList();
 list.Add(5);
 list.Add(7);
 // Second ArrayList.
 ArrayList list2 = new ArrayList();
 list2.Add(10);
 list2.Add(13);
 // Add second ArrayList to first.
 list.AddRange(list2);
 // Display the values.
 foreach (int i in list)
 {
     Console.WriteLine(i);
 }
    }
}
Output
5
7
10
13
Description of the two ArrayLists. The first ArrayList has two elements added
to it.Next,the second ArrayList has two elements added to it. The second ArrayList is then appended to the first using the AddRange method. The example finally show
the output.

Count and Clear

The ArrayList class provides the Count property, which is a virtual property. When you use Count, no counting is actually done; This means that Count is fairly fast. The example also shows Clear.
using System;
using System.Collections;
class Program
{
    static void Main()
    {
 // Create an ArrayList with three values.
 ArrayList list = new ArrayList();
 list.Add(9);
 list.Add(10);
list.Add(20);
 // Show number of elements in ArrayList.
 Console.WriteLine(list.Count);
 // Clear the ArrayList.
 list.Clear();
 // Show count again.
 Console.WriteLine(list.Count);
    }
}
Output
3
0
Description:
Count. The Count property returns an int, and you can say that it will always be a positive value.
Clear. You can call the method Clear on your ArrayList. Internally, this calls the Array.Clear method.

ArrayList Sort and Reverse

using System;
using System.Collections;
class Program
{
    static void Main()
    {
 // Create an ArrayList with five strings.
 ArrayList list = new ArrayList();
 list.Add("Cat");
 list.Add("Zebra");
 list.Add("Dog");
 list.Add("Cow");
list.Add("lion")
 // Sort the ArrayList.
 list.Sort();
 // Display the ArrayList elements.
 foreach (string value in list)
 {
     Console.WriteLine(value);
 }
 // Reverse the ArrayList.
  list.Reverse();
  // Display the ArrayList elements again.
  foreach (string value in list)
 {
     Console.WriteLine(value);
 }
    }
}

Output
Cat
Cow
Dog
Lion
Zebra

Zebra
Lion
Dog
Cow
Cat
Description:

Implementation:

Inside the base class libraries, you can see that the Sort method here always

ends up in an internal TrySZSort or QuickSort method when it doesn't throw an exception. The TrySZSort internal method is optimized for one-dimensional arrays, also known as "Zero" arrays or vectors.
Performance of Sort. Because the TrySZSort method used in the base class libraries is implemented in native code, it has been heavily optimized. Therefore, this method is likely faster than any solution written in the C# language. In other words: using Sort on ArrayList or on Array is faster than most custom implementations.

Insert and remove elements

using System;
using System.Collections;

class Program
{
    static void Main()
    {
 // Create an ArrayList with four strings.
 ArrayList list = new ArrayList();
 list.Add("Dot");
 list.Add("Net");
 list.Add("Perls");
list.Add("VB.Net");
        // Remove middle element in ArrayList.
 list.RemoveAt(1); // It becomes [Dot, Perls,Vb.Net]
 
 // Insert word at beginning of ArrayList.
 list.Insert(0, "Carrot"); // It becomes [Carrot, Dot, Perls,VB.Net]
 // Remove first two words from ArrayList.
 list.RemoveRange(0, 2);
 
 // Display the result ArrayList.
 foreach (string value in list)
 {
     Console.WriteLine(value); // <-- "Perls"
 }
    }
}

Output
Perls
Vb.Net

Loop with for

using System;
using System.Collections;
class Program
{
    static void Main()
    {
 // Create an ArrayList with four strings.
 ArrayList list = new ArrayList();
 list.Add("man");
 list.Add("woman");
 list.Add("plant");
list.Add("animals")
 // Loop over ArrayList with for.

 for (int i = 0; i < list.Count; i++)
 {
     string value = list[i] as string;
     Console.WriteLine(value);
 }
    }
}

Output

man
woman
plant
animal
Using "as" cast with ArrayList elements. The "as" cast in C# is probably the best way to cast reference types such as string. After you cast, you can check the result for null before using the variable, to see if the cast succeeded. This is not always needed.

Get range of values

using System;
using System.Collections;

class Program
{
    static void Main()
    {
 // Create an ArrayList with 4 strings.
 ArrayList list = new ArrayList();
 list.Add("fish");
 list.Add("amphibian");
 list.Add("bird");
 list.Add("plant");
 // Get last two elements in ArrayList.
 ArrayList range = list.GetRange(2, 2);
// Display the elements.
  foreach (string value in range)
 {
     Console.WriteLine(value); // bird, plant
 }
    }
}

Output

bird
plant
Hope This will help you 
Difference Between Array and ArrayList
  • Array is: a datatype, thatcan be used by calling indexes. during runtime, one cannot really change the size of the array, unless you use the method of copying the array and getting rid of the old one. In .NET, the Visual Studio makes use of a special class to store the data. Because of this, the performance is actually quite fast. This is also because in an array, you need to specify the size and thus, the data is stored one after the other.
  • Examples:  
      • int[ ] myNumbers= new int[5];
    • myNumbers[0] = 16;
  • ArrayList is: a datatype collection. In order to fill an ArrayList, one can use the .Add property. ArrayLists are very dynamic in the sense that when you add and/or remove items from it, the performace stays the same. The internal structure of an ArrayList is an array.
  • Examples:
    • ArrayList myArray = new ArrayList();
    • myArray .Add(“Steph”);
    • string str = myArray [0];
Most of the time, we tend to choose array lists rather than arrays since we have no idea how big it is going to turn out. Arrays are ideal when you know how many items you are going to put in it. Whenever possible, it is recommended to use arrays as this drastically improves the performance.
ARRAY
ARRAYLIST
1. Char[] vowel=new Char[]; ArrayList a_list=new ArrayList();
2. Array is in the System namespace ArrayList is in the System.Collections namespace.
3. The capacity of an Array is fixed ArrayList can increase and decrease size dynamically
4. An Array is a collection of similar items ArrayList can hold item of different types
5. An Array can have multiple dimensions  ArrayList always has exactly one dimension
Thanks 
Rohit Srivastava


4 comments: