当前位置: 移动技术网 > IT编程>开发语言>c# > C#将HashTable中键列表或值列表复制到一维数组的方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网