Demonstrate ArrayList : ArrayList « Collections Data Structure « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Class Interface
3. Collections Data Structure
4. Components
5. Data Types
6. Database ADO.net
7. Design Patterns
8. Development Class
9. Event
10. File Stream
11. Generics
12. GUI Windows Form
13. Language Basics
14. LINQ
15. Network
16. Office
17. Reflection
18. Regular Expressions
19. Security
20. Services Event
21. Thread
22. Web Services
23. Windows
24. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorial
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / C Sharp » Collections Data Structure » ArrayListScreenshots 
Demonstrate ArrayList
Demonstrate ArrayList

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Demonstrate ArrayList. 
 
using System; 
using System.Collections; 
 
public class ArrayListDemo 
  public static void Main() { 
    // create an array list 
    ArrayList al = new ArrayList()
     
    Console.WriteLine("Initial capacity: " 
                       al.Capacity)
    Console.WriteLine("Initial number of elements: " 
                       al.Count)
 
    Console.WriteLine()
 
    Console.WriteLine("Adding 6 elements")
    // Add elements to the array list 
    al.Add('C')
    al.Add('A')
    al.Add('E')
    al.Add('B')
    al.Add('D')
    al.Add('F')
 
    Console.WriteLine("Current capacity: " 
                       al.Capacity)
    Console.WriteLine("Number of elements: " 
                       al.Count)
 
    // Display the array list using array indexing. 
    Console.Write("Current contents: ")
    for(int i=0; i < al.Count; i++
      Console.Write(al[i" ")
    Console.WriteLine("\n")
 
    Console.WriteLine("Removing 2 elements")
    // Remove elements from the array list. 
    al.Remove('F')
    al.Remove('A')
 
    Console.WriteLine("Current capacity: " 
                       al.Capacity)
    Console.WriteLine("Number of elements: " 
                       al.Count)
 
    // Use foreach loop to display the list. 
    Console.Write("Contents: ")
    foreach(char c in al
      Console.Write(c + " ")
    Console.WriteLine("\n")
 
    Console.WriteLine("Adding 20 more elements")
    // Add enough elements to force al to grow. 
    for(int i=0; i < 20; i++
      al.Add((char)('a' + i))
    Console.WriteLine("Current capacity: " 
                       al.Capacity)
    Console.WriteLine("Number of elements after adding 20: " 
                       al.Count)
    Console.Write("Contents: ")
    foreach(char c in al
      Console.Write(c + " ")
    Console.WriteLine("\n")
 
    // Change contents using array indexing. 
    Console.WriteLine("Change first three elements")
    al[0'X'
    al[1'Y'
    al[2'Z'
    Console.Write("Contents: ")
    foreach(char c in al
      Console.Write(c + " ")
    Console.WriteLine()
  
}


           
       
Related examples in the same category
1. Add items to ArrayList and use foreach loop to check
2. Add array to ArrayList
3. Remove range from ArrayList
4. Clone an ArrayList
5. CopyTo, ToArray(), ToArray(typeof(String))
6. Using foreach to loop through ArrayList and use Indexer of ArrayList
7. Check InvalidCastException when using foreach loop with ArrayList
8. Binary Search an ArrayList
9. Inserting into an ArrayList by index
10. Checks time needed for list operations using an ArrayList implementationChecks time needed for list operations using an ArrayList implementation
11. ArrayList Demo: hold classArrayList Demo: hold class
12. illustrates the use of ArrayList properties and methodsillustrates the use of ArrayList properties and methods
13. illustrates the use of an ArrayList that contains objects of the Car classillustrates the use of an ArrayList that contains objects of the Car class
14. illustrates the use of ArrayLists 2illustrates the use of ArrayLists 2
15. Convert an ArrayList into an arrayConvert an ArrayList into an array
16. Sort and search an ArrayListSort and search an ArrayList
w_ww_.__j_a_v___a__2s_.c__om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.