C#数组复制操作
来源:互联网 发布:mac什么打开exe文件 编辑:程序博客网 时间:2024/06/05 04:32
这玩意真是难受,随便搞一下就云里雾里的……
本来系统里是有个Array.CopyTo方法的,但我不喜欢用,不合我的要求。
于是我就自己写,写着写着就绕里面了,这会儿刚绕出来,赶紧记一下。
(很杀笔的注释往往有奇效)
public static class HenShaBiDeArrayCopyTool { /// <summary> /// 小数组放进大数组里,从大数组的指定位置开始放小数组,小数组是从第0个开始取的。 /// 如果小数组没取完大数组到头了就不要再放了 /// </summary> /// <param name="small">小数组</param> /// <param name="large">大数组</param> /// <param name="index">从大数组的指定位置开始放小数组的那个指定位置</param> public static void WriteTo(this byte[] small, byte[] large, long index) { if (index >= large.LongLength) { throw new ArgumentOutOfRangeException("index out of large range"); } long slen = small.LongLength; long llen = large.LongLength; for (long i = index, j = 0; i < llen && j < slen; i++, j++) { large[i] = small[j]; } } /// <summary> /// 大数组放进小数组里,从大数组的指定位置开始取数据放到小数组里,小数组是从第0个开始放大数组的。 /// 如果大数组到头了就不要再取了,如果小数组到头了就不要再放了 /// </summary> /// <param name="large">大数组</param> /// <param name="small">小数组</param> /// <param name="index">从大数组的指定位置开始取数据放到小数组里的那个指定位置</param> public static void CopyTo(this byte[] large, byte[] small, long index) { if (index >= large.LongLength) { throw new ArgumentOutOfRangeException("index out of large range"); } long slen = small.LongLength; long llen = large.LongLength; for (long i = index, j = 0; i < llen && j < slen; i++, j++) { small[j] = large[i]; } } }
我觉得应该是原创,毕竟这么简单的东西……
第二个拓展方法和.Net里的重复了,改成这样:
/// <summary> /// 大数组放进小数组里,从大数组的指定位置开始取数据放到小数组里,小数组是从第0个开始放大数组的。 /// 如果大数组到头了就不要再取了,如果小数组到头了就不要再放了 /// </summary> /// <param name="small">小数组</param> /// <param name="large">大数组</param> /// <param name="index">从大数组的指定位置开始取数据放到小数组里的那个指定位置</param> public static void CopyFrom(this Array small, Array large, long index) { if (index >= large.LongLength) { throw new ArgumentOutOfRangeException("index out of large range"); } long slen = small.LongLength; long llen = large.LongLength; for (long i = index, j = 0; i < llen && j < slen; i++, j++) { object largedata = large.GetValue(i); small.SetValue(largedata, j); } }
last edited at 2016/12/02 15:40
如今再看,发现自己已经不是过度封装这么简单了。。。
//// Summary:// Copies all the elements of the current one-dimensional array to the specified// one-dimensional array starting at the specified destination array index.// The index is specified as a 32-bit integer.//// Parameters:// array:// The one-dimensional array that is the destination of the elements copied// from the current array.//// index:// A 32-bit integer that represents the index in array at which copying begins.//public void CopyTo(Array array, int index);
//// Summary:// Copies a range of elements from an System.Array starting at the first element// and pastes them into another System.Array starting at the first element.// The length is specified as a 32-bit integer.//// Parameters:// sourceArray:// The System.Array that contains the data to copy.//// destinationArray:// The System.Array that receives the data.//// length:// A 32-bit integer that represents the number of elements to copy.//public static void Copy(Array sourceArray, Array destinationArray, int length);
//// Summary:// Copies a range of elements from an System.Array starting at the specified// source index and pastes them to another System.Array starting at the specified// destination index. The length and the indexes are specified as 32-bit integers.//// Parameters:// sourceArray:// The System.Array that contains the data to copy.//// sourceIndex:// A 32-bit integer that represents the index in the sourceArray at which copying// begins.//// destinationArray:// The System.Array that receives the data.//// destinationIndex:// A 32-bit integer that represents the index in the destinationArray at which// storing begins.//// length:// A 32-bit integer that represents the number of elements to copy.//public static void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);
作为一个新手一定要多看官方文档,对提高很有帮助,而且如果是国外做出的东西就更要强迫英语水平不太好的自己去看英文文档。
0 0
- C#数组复制操作
- C#中数组复制
- C# 数组的复制
- C#中复制数组
- C#数组复制方法详解
- C#中数组的复制
- C# 复制文件夹的操作
- C# 字节数组操作
- C#字节数组操作
- c#数组的操作
- C#数组复制(合并)方法
- J2ME数组的复制及连接操作
- 数组操作-浅复制-《数据结构与算法》
- c#怎样实现复制/粘贴/操作?
- C#自定义集合List及复制操作
- C# 操作Excel ---复制Excel的sheet
- C#目录文件复制、创建操作
- C# winform文件夹操作复制和删除
- MVVM下DataGrid的简单应用
- 剑指offer面试题之二叉树中和为某一值的所有路径
- 求素数
- poj1017 最优解问题
- Android view中invalidate方法学习小节
- C#数组复制操作
- hdoj Snooker 2060 (水)
- 设计模式之简单工厂方法模式和工厂方法模式
- 轻松记住大端小端的含义(附对大端和小端的解释)
- HTTP状态码一览表
- 数据结构笔记及Java实现 (排序)
- XML语法及其约束
- LeetCode之旅(19)-Power of Two
- Spring的<aop:aspectj-autoproxy>配置