C#阵列移动项目(ArrayList不是/泛型列表) [英] C# Array Move Item (Not ArrayList/Generic List)

查看:69
本文介绍了C#阵列移动项目(ArrayList不是/泛型列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个对象,它是一个数组(ArrayList不是或通用),可以容纳一组什么...

  [一],[二],[三],[四]

要【四】移动到的【二】如前
oldIndex = 3,newIndex = 1
这样的结果将是...

  [一],[四] [二],[三]

请告诉我最effient方式做到这一点在.NET 2.0中,
例如。

 的PropertyInfo OPI = ObjectType.GetProperty(MYARRAY,BindingFlags.Public | BindingFlags.Instance);
反对ObjectToReorder = oPI.GetValue(ParentObject,NULL);
数组ARR = ObjectToReorder为阵;
INT oldIndex = 3;
INT newIndex = 1;
//需要仍连接到ParentObject重新排序的列表

在此先感谢


解决方案

 无效MoveWithinArray(数组的数组,INT源,INT DEST)
{
  对象TEMP = array.GetValue(源);
  Array.Copy(数组,DEST,数组,DEST + 1,源 - DEST);
  array.SetValue(温度,DEST);
}

Have an object which is an array (not arraylist or generic) that could hold a set of anything...

[[One],[Two],[Three],[Four]]

Want to move [Four] to in front of [Two] e.g. oldIndex = 3, newIndex = 1 so the result would be...

[[One],[Four][Two],[Three]]

Whats the most effient way to do this in .NET 2.0, e.g.

PropertyInfo oPI = ObjectType.GetProperty("MyArray", BindingFlags.Public | BindingFlags.Instance);
object ObjectToReorder = oPI.GetValue(ParentObject, null);
Array arr = ObjectToReorder as Array;
int oldIndex = 3;
int newIndex = 1;
//Need the re-ordered list still attached to the ParentObject

thanks in advance

解决方案

void MoveWithinArray(Array array, int source, int dest)
{
  Object temp = array.GetValue(source);
  Array.Copy(array, dest, array, dest + 1, source - dest);
  array.SetValue(temp, dest);
}

这篇关于C#阵列移动项目(ArrayList不是/泛型列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆