ArrayList
以下示例显示如何创建并初始化 ArrayList 以及如何打印出其值。 [Visual Basic] 
Imports System 
Imports System.Collections 
Imports Microsoft.VisualBasic Public Class SamplesArrayList     
     
    Public Shared Sub Main() 
         
        ' Creates and initializes a new ArrayList. 
        Dim myAL As New ArrayList() 
        myAL.Add("Hello") 
        myAL.Add("World") 
        myAL.Add("!") 
         
        ' Displays the properties and values of the ArrayList. 
        Console.WriteLine("myAL") 
        Console.WriteLine(ControlChars.Tab + "Count:    {0}", myAL.Count) 
        Console.WriteLine(ControlChars.Tab + "Capacity: {0}", myAL.Capacity) 
        Console.Write(ControlChars.Tab + "Values:") 
        PrintValues(myAL) 
    End Sub 
     
     
    Public Shared Sub PrintValues(myList As IEnumerable) 
        Dim myEnumerator As System.Collections.IEnumerator = _ 
           myList.GetEnumerator() 
        While myEnumerator.MoveNext() 
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current) 
        End While 
        Console.WriteLine() 
    End Sub 
End Class ' This code produces the following output. 

' myAL 
'     Count:    3 
'     Capacity: 16 
'     Values:    Hello    World    ! 
[C#] 
using System; 
using System.Collections; 
public class SamplesArrayList  {    public static void Main()  {       // Creates and initializes a new ArrayList. 
      ArrayList myAL = new ArrayList(); 
      myAL.Add("Hello"); 
      myAL.Add("World"); 
      myAL.Add("!");       // Displays the properties and values of the ArrayList. 
      Console.WriteLine( "myAL" ); 
      Console.WriteLine( "\tCount:    {0}", myAL.Count ); 
      Console.WriteLine( "\tCapacity: {0}", myAL.Capacity ); 
      Console.Write( "\tValues:" ); 
      PrintValues( myAL ); 
   }    public static void PrintValues( IEnumerable myList )  { 
      System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); 
      while ( myEnumerator.MoveNext() ) 
         Console.Write( "\t{0}", myEnumerator.Current ); 
      Console.WriteLine(); 
   } 

/* 
This code produces the following output. myAL 
    Count:    3 
    Capacity: 16 
    Values:    Hello    World    ! */ 

解决方案 »

  1.   

    Arraylistlook the sample:using System;
    using System.Collections;namespace Com
    { /// 
     ///Description for GSoundX.
     ///SoundX classifies words for how they sound by asssigning a short numeric
     ///code based on a group of letters.
     ///Take the first letter. 
     ///Then, for each of the subsequent letters, score a code for the group it 
     ///belongs to. There are 6 groups : 
     ///Group      Letters
     ///1          BFPV
     ///2          CGJKQSXZ
     ///3          DT
     ///4          L
     ///5          MN
     ///6          R
     ///Ignore all other letters and characters (vowels, h, punctuation, etc.)
     //////If a code has already occurred, ignore it. Continue until you have 4 characters,
     ///if there are less, add zeroes until you have 4.
     ///And they all sound the same or similar.
     /// 
     public class SoundX
     {  static ArrayList aList1 = new ArrayList(4);
      static ArrayList aList2 = new ArrayList(8);
      static ArrayList aList3 = new ArrayList(2) ;
      static ArrayList aList4 = new ArrayList(1);
      static ArrayList aList5 = new ArrayList(2);
      static ArrayList aList6 = new ArrayList(1);
      static ArrayList aListExcep = new ArrayList(6);  private static void setArrayLists()
      {
       
       aListExcep.Add("A");
       aListExcep.Add("E");
       aListExcep.Add("I");
       aListExcep.Add("O");
       aListExcep.Add("U");
       aListExcep.Add("H");
       
       aList1.Add("B");
       aList1.Add("F");
       aList1.Add("P");
       aList1.Add("V");   aList2.Add("C");
       aList2.Add("G");
       aList2.Add("J");
       aList2.Add("K");
       aList2.Add("Q");
       aList2.Add("S");
       aList2.Add("X");
       aList2.Add("Z");
      
       aList3.Add("D");
       aList3.Add("T");
       
       aList4.Add("L");
       
       aList5.Add("M");
       aList5.Add("N");   aList6.Add("R");
      
      }  /// 
      /// This method does the core functionality of 
      /// geting the soundx code for the  giving string 
      /// 
      /// The string value for which SoundX equivalent is needed.
      /// SoundX output string.
      public static String getSoundEX(String strInput)
      {
       setArrayLists();
       String strReturn = "";
       try
       {
        String strTemp = strInput.Substring(0,1).ToUpper();
        if(strInput.Length > 0)
        {
         int iStrLen = 0;
         if(strInput.Length >= 4)
         {
          iStrLen = 4;
         }
         else
         {
          iStrLen = strInput.Length;
         }
         char[] chrEachChar = strInput.ToCharArray(0,iStrLen);
         ArrayList aListTemp = new ArrayList(iStrLen);
         foreach(char chr in chrEachChar)
         {
          String strTempChar = chr.ToString().ToUpper();
          
          bool blnGoOn = true;
          //check if the character has been already counted
          if(aListTemp.Count > 0)
          {
           if(aListTemp.IndexOf(strTempChar) >= 0)
           {
            blnGoOn = false;
           }
          }
          else
          {
           //do not go in for the first character
           blnGoOn = false;
          }
          //add the char to the array so that
          //the consequetive entries are checked for.
          aListTemp.Add(strTempChar);      if(blnGoOn == true)
          {
           if(aListExcep.IndexOf(strTempChar) < 0 ) //if the char is not a vowel or H
           {
            if(aList1.IndexOf(strTempChar)< 0)
            {
             if(aList2.IndexOf(strTempChar)< 0)
             {
              if(aList3.IndexOf(strTempChar)< 0)
              {           if(aList4.IndexOf(strTempChar) < 0)
               {
                if(aList5.IndexOf(strTempChar) < 0)
                {
                 if(aList6.IndexOf(strTempChar) >= 0)
                 {
                  strTemp = strTemp + "6";
                 }
                }
                else //array5
                {
                 strTemp = strTemp + "5";
                }
               }
               else //array4
               {
                strTemp = strTemp + "4";
               }
              }
              else //array3
              {
               strTemp = strTemp + "3";
              }
             }
             else //array2
             {
              strTemp = strTemp + "2";
             }
            }
            else //array1
            {
             strTemp = strTemp + "1";
            }
           }//if the char is a vowel or H
          }//if the char has already been taken into account
         }//end for each
         if(strTemp.Length < 5)
         {
          int iLen = strTemp.Length;
          if(iLen == 1)
          {
           strTemp = strTemp + "0000";
          }
          else if(iLen == 2)
          {
           strTemp = strTemp + "000";
          }
          else if(iLen == 3)
          {
           strTemp = strTemp + "00";
          }
          else if(iLen == 4)
          {
           strTemp = strTemp + "0";
          }     }
         strReturn = strTemp.Trim();
        }
        else
        {
         strReturn = "";
        }
       
       }
       catch(Exception exp)
       {
        strReturn = "ERR:" + exp.ToString();
       }
       return(strReturn);
      }
     }
    }