PlotMyFace.Location.SwapLocations C# (CSharp) Метод

SwapLocations() публичный статический Метод

public static SwapLocations ( Location locations, int index1, int index2 ) : void
locations Location
index1 int
index2 int
Результат void
        public static void SwapLocations(Location[] locations, int index1, int index2)
        {
            if (locations == null)
                throw new ArgumentNullException("locations");

            if (index1 < 0 || index1 >= locations.Length)
                throw new ArgumentOutOfRangeException("index1");

            if (index2 < 0 || index2 >= locations.Length)
                throw new ArgumentOutOfRangeException("index2");

            var location1 = locations[index1];
            var location2 = locations[index2];
            locations[index1] = location2;
            locations[index2] = location1;
        }

Usage Example

Пример #1
0
        public static void FullyRandomizeLocations(Location[] locations)
        {
            if (locations == null)
            {
                throw new ArgumentNullException("locations");
            }

            // This code does a full randomization of the destination locations without creating a new array.
            // If we have 3 items, for example, it will first determine which one of the 3 will be in the last
            // place, swapping items if needed. Then, it will chose which one of the first 2 items is put at the
            // second place. And, as everything works by swaps, the item in the first position is obviously the
            // only one that remains, that's why the i>0 is used instead of i>=0.
            int count = locations.Length;

            for (int i = count - 1; i > 0; i--)
            {
                int value = GetRandomValue(i + 1);
                if (value != i)
                {
                    Location.SwapLocations(locations, i, value);
                }
            }
        }
All Usage Examples Of PlotMyFace.Location::SwapLocations