欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#将HashTable中键列表或值列表复制到一维数组的方法

程序员文章站 2022-03-22 14:27:02
本文实例讲述了c#将hashtable中键列表或值列表复制到一维数组的方法。分享给大家供大家参考。具体如下: 下面的示例说明如何将 hashtable 中键的列表或值的列...

本文实例讲述了c#将hashtable中键列表或值列表复制到一维数组的方法。分享给大家供大家参考。具体如下:

下面的示例说明如何将 hashtable 中键的列表或值的列表复制到一维 array 中。

using system;
using system.collections;
public class sampleshashtable {
 public static void main() {
  // creates and initializes the source hashtable.
  hashtable mysourceht = new hashtable();
  mysourceht.add( "a", "valuea" );
  mysourceht.add( "b", "valueb" );
  // creates and initializes the one-dimensional target array.
  string[] mytargetarray = new string[15];
  mytargetarray[0] = "the";
  mytargetarray[1] = "quick";
  mytargetarray[2] = "brown";
  mytargetarray[3] = "fox";
  mytargetarray[4] = "jumped";
  mytargetarray[5] = "over";
  mytargetarray[6] = "the";
  mytargetarray[7] = "lazy";
  mytargetarray[8] = "dog";
  // displays the values of the target array.
  console.writeline( "the target array contains the following before:" );
  printvalues( mytargetarray, ' ' );
  // copies the keys in the source hashtable to the target hashtable, starting at index 6.
  console.writeline( "after copying the keys, starting at index 6:" );
  mysourceht.keys.copyto( mytargetarray, 6 );
  // displays the values of the target array.
  printvalues( mytargetarray, ' ' );
  // copies the values in the source hashtable to the target hashtable, starting at index 6.
  console.writeline( "after copying the values, starting at index 6:" );
  mysourceht.values.copyto( mytargetarray, 6 );
  // displays the values of the target array.
  printvalues( mytargetarray, ' ' );
 }
 public static void printvalues( string[] myarr, char myseparator ) {
  for ( int i = 0; i < myarr.length; i++ )
   console.write( "{0}{1}", myseparator, myarr[i] );
  console.writeline();
 }
}
/* 
this code produces the following output.
the target array contains the following before:
 the quick brown fox jumped over the lazy dog
after copying the keys, starting at index 6:
 the quick brown fox jumped over b a dog
after copying the values, starting at index 6:
 the quick brown fox jumped over valueb valuea dog
*/

希望本文所述对大家的c#程序设计有所帮助。