C# 不使用“list.Reverse()”反转字符串列表的顺序
有人能告诉我如何在不使用list.reverse的情况下颠倒字符串列表的顺序吗 例如: 1.母牛 2.猫 3.狗 反转后: 4.狗 5.猫 6.母牛 降序就行了 给你:C# 不使用“list.Reverse()”反转字符串列表的顺序,c#,C#,有人能告诉我如何在不使用list.reverse的情况下颠倒字符串列表的顺序吗 例如: 1.母牛 2.猫 3.狗 反转后: 4.狗 5.猫 6.母牛 降序就行了 给你: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { class Program {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
List<string> arrWords = new List<string>();
while (true)
{
Console.Write("Type a word: ");
string arrInput = Console.ReadLine();
if (arrInput == "")
{
break;
}
arrWords.Add(arrInput);
}
for (int i = 0; i < arrWords.Count; i++)
{
for (int j = i + 1; j < arrWords.Count; j++)
{
if (arrWords[i] > arrWords[j])
{
string temp = arrWords[i];
arrWords[i] = arrWords[j];
arrWords[j] = temp;
}
}
}
foreach (string arrWord in arrWords)
{
Console.WriteLine(arrWord);
}
}
}
}
myList=myList.OrderByDowneringX=>x.ToList?您为什么不想使用反向?你会用linq吗?或者你的实际需求是什么?反转你的循环。为什么反向订购时牛在猫后面?这感觉像是你的家庭作业,可能是重复的
class Program
{
static void Main(string[] args)
{
// 1. Cow 2. Cat 3. Dog After Reverse: 4. Dog 5. Cat 6. Cow
List<string> list = new List<string>() { "Cow", "Cat", "Dog" };
ReverseList(list);
}
private static void ReverseList(List<string> list)
{
int i = 0;
int j = list.Count - 1;
for (i = 0; i < list.Count / 2; i++, j--)
{
string temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
int i = 0;
int j = arrWords.Length - 1;
while (i < j)
{
string temp = arrWords[i];
objArray[i] = arrWords[j];
objArray[j] = temp;
i++;
j--;
}