RT 
在一个数组里放了几张图片 怎么把指定的一张图片 放到数组的第一个位置  现在是按照图片名字顺序放的
要怎么写啊

解决方案 »

  1.   

    public static class ArrHelper
     {
             /// <summary>
             /// 对类型为T的数组进行扩展,把满足条件的元素移动到数组的最前面
            /// </summary>
             /// <typeparam name="T"></typeparam>
             /// <param name="arr">源数组</param>
             /// <param name="match">lamda表达式</param>
             /// <returns></returns>
             public static bool MoveToFront<T>(this T[] arr, Predicate<T> match)
             {
                 //如果数组的长度为0
                 if (arr.Length == 0)
                 {
                     return false;
                 }
                 //获取满足条件的数组元素的索引
                var index = Array.FindIndex(arr, match);
                 //如果没有找到满足条件的数组元素
                if (index == -1)
                 {
                     return false;
                 }
                 //把满足条件的数组元素赋值给临时变量
                var temp = arr[index];
                 Array.Copy(arr, 0, arr, 1, index);
                 arr[0] = temp;
                 return true;
             }
             public static void PrintArray<T>(T[] arr)
             {
                 foreach (var item in arr)
                 {
                     Console.Write(item + " ");
                 }
                 Console.WriteLine();
             }
     }
    class Program
     {
             static void Main(string[] args)
             {
                 int[] intArr = new int[]{1, 2, 3, 4, 5};
                 ArrHelper.PrintArray(intArr);
                 intArr.MoveToFront(i => i == 3);
                 ArrHelper.PrintArray(intArr);
                 Console.ReadKey();
             }
     }
      

  2.   

    array.insert 0
      

  3.   

    new之后
    arr[10]=xxx
      

  4.   

    把指定的元素与第一个元素之间进行swap就可以了
      

  5.   

    为什么不用List<T>?
      

  6.   

    转换为list,插入过后再转换回来
      

  7.   

                string[] a = { "A", "B", "C", "D" };
                var n = Array.IndexOf(a, "C");
                var b = a[n];
                Array.Copy(a, 0, a, 1, n);
                a[0] = b;
     
      

  8.   

    Image[] arr = { ... }
    Image t = arr[x];
    arr[x] = arr[0];
    arr[0] = t;