下面的代码示例定义一个名为 PointFToPoint 的方法,该方法可将 PointF 结构转换为 Point 结构。然后,该示例创建一个 PointF 结构的数组和一个 Converter<PointF, Point> 委托(在 Visual Basic 中为 Converter(Of PointF, Point))来表示 PointFToPoint 方法,并将该委托传递给 ConvertAll 方法。ConvertAll 方法将输入列表中的每个元素传递给 PointFToPoint 方法,并将转换后的元素置于 Point 结构的新列表中。这两个列表都会显示。using System; using System.Drawing; using System.Collections.Generic;public class Example { public static void Main() { PointF[] apf = { new PointF(27.8F, 32.62F), new PointF(99.3F, 147.273F), new PointF(7.5F, 1412.2F) }; Console.WriteLine(); foreach( PointF p in apf ) { Console.WriteLine(p); } Point[] ap = Array.ConvertAll(apf, new Converter<PointF, Point>(PointFToPoint)); Console.WriteLine(); foreach( Point p in ap ) { Console.WriteLine(p); } } public static Point PointFToPoint(PointF pf) { return new Point(((int) pf.X), ((int) pf.Y)); } }/* This code example produces the following output:{X=27.8, Y=32.62} {X=99.3, Y=147.273} {X=7.5, Y=1412.2}{X=27,Y=32} {X=99,Y=147} {X=7,Y=1412} */
用Array.ConvertAll 范型方法。
最简单的方法,知道数组怎么复制吧? 照着思路写呗string[] finalData = new string[16] { "1", "2", "提问", "五笔", "3", "4", "5", "6", "2", "4", "7", "8", "9", "4", "5", "2" }; datatable tb = new datatable(); tb.columns.add("name"); for(int i = 0; i < finalData.length;i++) { datarow dr = tb.newrow(); dr["name"] = finalData[i]; tb.row.add(dr); }手写的,可能报错
//通常的引用类型都有自己的格式,是不能够强制转换的(绝大多数能够强转的都是值类型)
string[] finalData = new string[16] { "1", "2", "提问", "五笔", "3", "4", "5", "6", "2", "4", "7", "8", "9", "4", "5", "2" };
//如果要生成一个DataRow必须要生成他的结构
DataTable dt = new DataTable();
dt.Columns.Add("index");
//这样DataRow 才有了1列
DataRow dr = dt.NewRow();//通常都是由dataTable生成,这样就有了和其一样的列结构
//当他有了和你要转换相同的结构后 用循环赋值
for (int i = 0; i < finalData.Length; i++)
{
dr[i] = finalData[i];
}
//最好了解一下基础类型
//详细研究一下dataTable
string[] finalData = new string[16] { "1", "2", "提问", "五笔", "3", "4", "5", "6", "2", "4", "7", "8", "9", "4", "5", "2" };
添加到已定义好的dataTable的dataRow 中,如何处理?
1楼的,你的这句dr[i] = finalData[i]; DataRow dr = dt.NewRow();数据类型还是不一样!
[]只是映射到一个方法而已
string[] finalData = new string[16] { "1", "2", "提问", "五笔", "3", "4", "5", "6", "2", "4", "7", "8", "9", "4", "5", "2" };
添加到dataTable中 ,用for语句怎么写。用别的语句写能实现也行 !
using System.Drawing;
using System.Collections.Generic;public class Example
{
public static void Main()
{
PointF[] apf = {
new PointF(27.8F, 32.62F),
new PointF(99.3F, 147.273F),
new PointF(7.5F, 1412.2F) }; Console.WriteLine();
foreach( PointF p in apf )
{
Console.WriteLine(p);
} Point[] ap = Array.ConvertAll(apf,
new Converter<PointF, Point>(PointFToPoint)); Console.WriteLine();
foreach( Point p in ap )
{
Console.WriteLine(p);
}
} public static Point PointFToPoint(PointF pf)
{
return new Point(((int) pf.X), ((int) pf.Y));
}
}/* This code example produces the following output:{X=27.8, Y=32.62}
{X=99.3, Y=147.273}
{X=7.5, Y=1412.2}{X=27,Y=32}
{X=99,Y=147}
{X=7,Y=1412}
*/
tb.columns.add("name");
for(int i = 0; i < finalData.length;i++)
{
datarow dr = tb.newrow();
dr["name"] = finalData[i];
tb.row.add(dr);
}手写的,可能报错