代码之家  ›  专栏  ›  技术社区  ›  Mike

如何在C#中移动数组的开头?

  •  5
  • Mike  · 技术社区  · 16 年前

    我试图根据值的第一次出现来重新组织数组(从而模拟与圆形数组类似的功能。)

    int[] myArray = {2, 3, 6, 1, 7, 6};
    

    myArray = {6, 1, 7, 6, 2, 3};
    

    实现这一目标的“最佳”方式是什么?

    8 回复  |  直到 16 年前
        1
  •  19
  •   Lynn Crumbling    11 年前
    int[] myArray = { 2, 3, 6, 1, 7, 6 };
    myArray = myArray
                .SkipWhile(i => i != 6)
                .Concat(myArray.TakeWhile(i => i != 6))
                .ToArray();
    

    应该耍花招!

    System.Linq ;

        2
  •  9
  •   Jon Skeet    16 年前

    Thorsten的解决方案创建了一个新的数组;这里有一个就地版本,它只创建一个与旋转大小一样大的临时数组:

    public static void RotateLeft<T>(T[] array, int places)
    {
        T[] temp = new T[places];
        Array.Copy(array, 0, temp, 0, places);
        Array.Copy(array, places, array, 0, array.Length - places);
        Array.Copy(temp, 0, array, array.Length - places, places);
    }
    

    可以

    public static void RotateLeft<T>(T[] array)
    {
        T temp = array[0];
        Array.Copy(array, 0, array, 1, array.Length - 1);
        array[array.Length-1] = temp;
    }
    
        3
  •  5
  •   Thorsten Dittmar    16 年前

    您可以执行以下操作:

    1. 确定你的“开始指数”
    2. Array.Copy() 将源数组从开始索引到结束的所有内容复制到目标数组

    通过这种方式,您可以获得一个看起来与预期一致的源数组副本。

    你必须应对各种过载

        4
  •  5
  •   Joren    16 年前

    // value contains the value to find.
    
    int skip;
    for (int i = 0; i < array.Length; i++)
    {
        if (array[i] == value)
        {
            skip = i;
            break;
        }
    }
    
    // skip contains the index of the element to put at the front.
    // Equivalently, it is the number of items to skip.
    // (I chose this name for it because it makes the subtractions
    // in the Array.Copy implementation more intuitive.)
    

    您想更改实际数组吗?然后按照Thorsten Dittmar的建议去做:

    int[] array = new int[] { 2, 3, 6, 1, 7, 6 };
    int[] result = new int[array.Length];
    
    int skip = 2; // So that array[skip] will be result[0] at the end
    
    Array.Copy(array, skip, result, 0, array.Length - skip);
    Array.Copy(array, 0, result, array.Length - skip, skip);
    

    你想只是 视图 将数组按新顺序排列,而不做其他任何事情?然后按如下方式对其进行索引:

    array[(i + skip) % array.Length]  // Instead of array[i]
    

    sourceValue

    // GCD gives the greatest common divisor
    int gcd = GCD(array.Length, skip);
    
    // period is the length of the permutation cycles in our rotation.
    int period = array.Length / gcd;
    
    int max = array.Length / period;
    for (int i = 0; i < max; i++)
    {
        int sourceIndex = i;
        int sourceValue = array[sourceIndex];
    
        for (int n = 1; n <= period; n++)
        {
            int destinationIndex = (sourceIndex + array.Length - skip) % array.Length;
    
            int temp = array[destinationIndex];
            array[destinationIndex] = sourceValue;
            sourceValue = temp;
    
            sourceIndex = destinationIndex;
        }
    }
    
        5
  •  3
  •   Juliet    16 年前

    class CircularList<T> : IList<T>
    {
        static IEnumerable<T> ToEnumerator(CircularList<T> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                yield return list[i];
            }
        }
    
        IList<T> arr;
        public int Shift { get; private set; }
        public CircularList(IList<T> arr, int shift)
        {
            this.arr = arr;
            this.Shift = shift;
        }
    
        int shiftIndex(int baseIndex)
        {
            return (baseIndex + Shift) % arr.Count;
        }
    
        #region IList<T> Members
    
        public int IndexOf(T item) { throw new NotImplementedException(); }
        public void Insert(int index, T item) { throw new NotImplementedException(); }
        public void RemoveAt(int index) { throw new NotImplementedException(); }
        public T this[int index]
        {
            get { return arr[shiftIndex(index)]; }
            set { arr[shiftIndex(index)] = value; }
        }
    
        #endregion
    
        #region ICollection<T> Members
    
        public void Add(T item) { throw new NotImplementedException(); }
        public void Clear() { throw new NotImplementedException(); }
        public bool Contains(T item) { throw new NotImplementedException(); }
        public void CopyTo(T[] array, int arrayIndex) { throw new NotImplementedException(); }
        public int Count { get { return arr.Count; } }
        public bool IsReadOnly { get { throw new NotImplementedException(); } }
        public bool Remove(T item) { throw new NotImplementedException(); }
    
        #endregion
    
        #region IEnumerable<T> Members
    
        public IEnumerator<T> GetEnumerator()
        {
            return ToEnumerator(this).GetEnumerator();
        }
    
        #endregion
    
        #region IEnumerable Members
    
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ToEnumerator(this).GetEnumerator();
        }
    
        #endregion
    }
    

    class Program
    {
        static void Main(string[] args)
        {
            int[] myArray = { 2, 3, 6, 1, 7, 6 };
            CircularList<int> circularList =
                new CircularList<int>(myArray, Array.IndexOf<int>(myArray, 6));
    
            foreach (int i in circularList)
            {
                Console.WriteLine(i);
            }
        }
    }
    

    6
    1
    7
    6
    2
    3
        6
  •  0
  •   Aleksandar Vucetic    16 年前
    var ar = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    ar = ar.SkipWhile(a => a != 6).ToArray<int>();
    
        7
  •  0
  •   Vikas    13 年前

    C#答案: 输入:{1,2,3,5,6,7,8};

    {
        static void Main(string[] args)
        {
            int[] array = { 1, 2, 3, 5, 6, 7, 8 };
            int index = 2;
            int[] tempArray = new int[array.Length];
            array.CopyTo(tempArray, 0);
    
            for (int i = 0; i < array.Length - index; i++)
            {
                array[index + i] = tempArray[i];
            }
    
            for (int i = 0; i < index; i++)
            {
                array[i] = tempArray[array.Length -1 - i];
            }            
    
    
        }
    }
    
        8
  •  0
  •   Jeppe Stig Nielsen    10 年前

      const int value = 6;
      int[] myArray = { 2, 3, 6, 1, 7, 6, };
    
      var index = Array.IndexOf(myArray, value);
      if (index == -1)
        throw new InvalidOperationException();
    
      var rotatedArray = (new ArraySegment<int>(myArray, index, myArray.Length - index))
        .Concat(new ArraySegment<int>(myArray, 0, index))
        .ToArray();
    

    ArraySegment<> IEnumerable<>


      rotatedArray.CopyTo(myArray, 0);