C# 如何使用数组将此旋转方法转换为使用列表?
我有一种方法,它使用数组旋转值,同时将占据最终索引的值保持在相同的位置 我如何使用列表做同样的事情C# 如何使用数组将此旋转方法转换为使用列表?,c#,arrays,list,C#,Arrays,List,我有一种方法,它使用数组旋转值,同时将占据最终索引的值保持在相同的位置 我如何使用列表做同样的事情 /* * Maintains the value and index of the last value in the array, while rotating the remaining values */ private static Team[] rotateTeams(Team[] teamsArray) { // ca
/*
* Maintains the value and index of the last value in the array, while rotating the remaining values
*/
private static Team[] rotateTeams(Team[] teamsArray)
{
// capture last value in array
Team lastPosition = teamsArray[teamsArray.Length - 1];
// create a smaller array to hold remaining values
Team[] smallerTeamArray = new Team[teamsArray.Length - 1];
for (int i = 0; i < smallerTeamArray.Length; i++)
{
smallerTeamArray[i] = teamsArray[i];
}
// create a temporary smaller array to rotate values
Team[] tempTeamsArray = new Team[smallerTeamArray.Length];
// choose how many positions to rotate
int rotate = 1;
// populate temp array with rotated values
for (int i = 0; i < smallerTeamArray.Length; i++)
{
tempTeamsArray[i] = smallerTeamArray[(i + rotate) % smallerTeamArray.Length];
}
// repopulate larger array with values and positions from smaller temp array
for (int i = 0; i < teamsArray.Length - 1; i++)
{
teamsArray[i] = tempTeamsArray[i];
}
// add captured last value from above to final position in array
teamsArray[teamsArray.Length - 1] = lastPosition;
return teamsArray;
}
/*
*保持数组中最后一个值的值和索引,同时旋转其余值
*/
专用静态团队[]rotateTeams(团队[]teamsArray)
{
//捕获数组中的最后一个值
Team lastPosition=teamsArray[teamsArray.Length-1];
//创建一个较小的数组以保存剩余值
Team[]smallerTeamArray=新团队[teamsArray.Length-1];
for(int i=0;i
您应该有这样的更改
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;
}
私有静态列表rotateTeams(列表团队阵列)
{
//捕获数组中的最后一个值
Team lastPosition=teamsArray[teamsArray.Count-1];
//创建一个较小的数组以保存剩余值
List smallerTeamArray=新列表();
对于(int i=0;i
通过更改方法声明以获取列表,并将长度更改为计数
?严重吗?你真的认为对这个问题的评论还不够吗?smallerTeamArray的循环i=0,count=0,rest看起来还可以,将用于创建较小的列表