使用Array.CopyTo(array,index)方法public class SamplesArray2{ public static void Main() {
// Creates and initializes the source Array.
Array myArrayZero=Array.CreateInstance( typeof(String), 3 );
myArrayZero.SetValue( "zero", 0 );
myArrayZero.SetValue( "one", 1 ); // Displays the source Array.
Console.WriteLine( "The array with lower bound=0 contains:" );
PrintIndexAndValues( myArrayZero ); // Creates and initializes the target Array.
int[] myArrLen = { 4 };
int[] myArrLow = { 2 };
Array myArrayTwo=Array.CreateInstance( typeof(String), myArrLen, myArrLow );
myArrayTwo.SetValue( "two", 2 );
myArrayTwo.SetValue( "three", 3 );
myArrayTwo.SetValue( "four", 4 );
myArrayTwo.SetValue( "five", 5 ); // Displays the target Array.
Console.WriteLine( "The array with lower bound=2 contains:" );
PrintIndexAndValues( myArrayTwo ); // Copies from the array with lower bound=0 to the array with lower bound=2.
myArrayZero.CopyTo( myArrayTwo, 3 ); // Displays the modified target Array.
Console.WriteLine( "\nAfter copying to the target array from index 3:" );
PrintIndexAndValues( myArrayTwo );
}
public static void PrintIndexAndValues( Array myArray ) {
for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
}
}
/*
This code produces the following output.The array with lower bound=0 contains:
[0]: zero
[1]: one
[2]:
The array with lower bound=2 contains:
[2]: two
[3]: three
[4]: four
[5]: fiveAfter copying to the target array from index 3:
[2]: two
[3]: zero
[4]: one
[5]:
*/
// Creates and initializes the source Array.
Array myArrayZero=Array.CreateInstance( typeof(String), 3 );
myArrayZero.SetValue( "zero", 0 );
myArrayZero.SetValue( "one", 1 ); // Displays the source Array.
Console.WriteLine( "The array with lower bound=0 contains:" );
PrintIndexAndValues( myArrayZero ); // Creates and initializes the target Array.
int[] myArrLen = { 4 };
int[] myArrLow = { 2 };
Array myArrayTwo=Array.CreateInstance( typeof(String), myArrLen, myArrLow );
myArrayTwo.SetValue( "two", 2 );
myArrayTwo.SetValue( "three", 3 );
myArrayTwo.SetValue( "four", 4 );
myArrayTwo.SetValue( "five", 5 ); // Displays the target Array.
Console.WriteLine( "The array with lower bound=2 contains:" );
PrintIndexAndValues( myArrayTwo ); // Copies from the array with lower bound=0 to the array with lower bound=2.
myArrayZero.CopyTo( myArrayTwo, 3 ); // Displays the modified target Array.
Console.WriteLine( "\nAfter copying to the target array from index 3:" );
PrintIndexAndValues( myArrayTwo );
}
public static void PrintIndexAndValues( Array myArray ) {
for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
}
}
/*
This code produces the following output.The array with lower bound=0 contains:
[0]: zero
[1]: one
[2]:
The array with lower bound=2 contains:
[2]: two
[3]: three
[4]: four
[5]: fiveAfter copying to the target array from index 3:
[2]: two
[3]: zero
[4]: one
[5]:
*/
解决方案 »
- c# SerialPort 接收数据不连续
- 如何把DataGridView1的数据从一个窗体1复制到另一个窗体2的DataGridView2
- WPF和webBrower做无边框浏览器?
- 如何计算字符串
- 远程启动程序
- 高手进来看看怎样解决..
- C#向Excel导入数据,不同的数据导入到不同的sheet中去(如一个表前10条导入到sheet1,后10条导入到sheet2。。。)
- 第一个解决的给50分
- 我想在大连地区找一个好的讲C#老师!!有兴趣的近来看一下
- 如何将treeview中的treenode节点内容保存为标准格式的xml文件?
- 一个基于B/S模式的MIS系统,我想用.net ,C#和SqlServer2000来实现应该注意那些方面?需要使用那些技术?
- 请教各位,如何将windows程序转成web程序,急!
a.CopyTo(b,index);