如何使用数组转换此旋转方法而不是使用列表?

时间:2017-10-21 08:32:46

标签: c# arrays list

我有一个方法,它使用数组来旋转值,同时保持值占据最终索引在同一位置。

如何使用列表执行相同的操作?

modules

1 个答案:

答案 0 :(得分:1)

你应该有这样的改变

private static List<Team> rotateTeams(List<Team> teamsArray)
        {
            // capture last value in array 
            Team lastPosition = teamsArray[teamsArray.Count - 1];
            // create a smaller array to hold remaining values
            List<Team> smallerTeamArray = new List<Team>();
            for (int i = 0; i < smallerTeamArray.Count; i++)
            {
                smallerTeamArray[i] = teamsArray[i];
            }
            // create a temporary smaller array to rotate values
            List<Team> tempTeamsArray = new List<Team>();
            // choose how many positions to rotate
            int rotate = 1;
            // populate temp array with rotated values
            for (int i = 0; i < smallerTeamArray.Count; i++)
            {
                tempTeamsArray[i] = smallerTeamArray[(i + rotate) % smallerTeamArray.Count];
            }
            // repopulate larger array with values and positions from smaller temp array
            for (int i = 0; i < teamsArray.Count - 1; i++)
            {
                teamsArray[i] = tempTeamsArray[i];
            }
            // add captured last value from above to final position in array
            teamsArray[teamsArray.Count - 1] = lastPosition;
            return teamsArray;
        }