简体   繁体   English

C#在不使用sort()或reverse()的情况下反转字符串数组

[英]C# Reverse a string array without using sort( ) or reverse( )

Hi I am trying to write a method that will reverse a string array onced called. 嗨,我正在尝试编写一种方法,该方法将反转一次调用的字符串数组。 I finished my code , but only get half of the array reversed, leaving the rest unchanged, being stuck on this for hours. 我完成了代码,但仅使数组的一半反转,其余部分保持不变,停留了数小时。 so I had to ask on stack as last resort. 所以我不得不在栈上问问作为不得已的方法。

int start;
string[] sArray = {
    "Reverse", "this", "string", "type", "array"
};
int end = (sArray.Length - 1);
for (start = 0; start < sArray.Length; start++) {
    sArray[start] = sArray[end - start];

    Console.Write(sArray[start] + ",");
}

//  The output supposed to be : array type string this Reverse
// However, I keep getting array type string type array.
//  The output supposed to be : array type string this Reverse
// However, I keep getting array type string type array.

Any ideas would be appreciated. 任何想法,将不胜感激。

You are missing swapping. 您缺少交换。 And you can do it with half of len of array: 您可以使用数组的len的一半进行操作:

string[] sArray = { "Reverse", "this", "string", "type", "array" };

for (int start = 0; start < sArray.Length/2; start++ )
{
     var temp = sArray[start];
     sArray[start] = sArray[sArray.Length - 1 - start];
     sArray[sArray.Length - 1 - start] = temp;                    
}

There are lots of way to do this. 有很多方法可以做到这一点。

First, you can use recursion. 首先,您可以使用递归。 In C#-like pseudocode this will look like this: 在类似C#的伪代码中,它将如下所示:

T[] Reverse<T>(T[] input)
{
    if(input.Length <= 1) return input;
    return Reverse(input[0..input.Length - 1]) + input[..input.Length];
}

Next is an in-place reverse; 接下来是就地反向; almost what you've already done, except your for loop is twice as big as it needs to be. 几乎已经完成的工作,除了for循环是需要的两倍。 See what happens when you change one of the parts of your loop to start < sArray.Length / 2 . 看看更改循环的一部分之一以start < sArray.Length / 2时会发生什么。 Plus, you really need to swap the elements. 另外,您确实需要交换元素。

You are rewriting items after the HALF to the first half. 您正在将HALF之后的内容重写到上半部分。 try this: 尝试这个:

int start;
string[] sArray = { "Reverse", "this", "string", "type", "array" };
string[] temp = new string[sArray.Length];
int end = (sArray.Length-1);
for (start = 0; start < sArray.Length; start++ )
            {
                temp[start] = sArray[end - start];

                Console.Write(sArray[start]+",");
            }
sArray = temp; // putting back to the original varible

After half of the table you're swapping elements with already swapped. 在表的一半之后,您要交换已交换的元素。 The proper solution is: 正确的解决方案是:

    static void Reverse()
    {
        string[] sArray = { "Reverse", "this", "string", "type", "array" };
        int end = sArray.Length - 1;
        for (int start = 0; start < (sArray.Length / 2); ++start)
        {
            string tmp = sArray[start];
            sArray[start] = sArray[end - start];
            sArray[end - start] = tmp;
        }

        foreach (var s in sArray)
            Console.Write(s + ",");
    }

Hopefully this answer makes sense, you can replace T by string if you don't understand generics. 希望这个答案有意义,如果您不了解泛型,则可以用字符串替换T The end variable starts at the end, starts starts at the front of the array, and they progress closer to eachother until they point to the same element (for an odd sized array) or the end pointer points to something before the start pointer(for an even sized array), in either case start < end will return false, and the loop will stop. end变量从末尾starts从数组的starts开始,并且它们彼此靠近,直到它们指向相同的元素(对于奇数大小的数组)或结束指针指向开始指针之前的某物(对于偶数大小的数组),无论哪种情况, start < end都将返回false,并且循环将停止。

private static void Reverse<T>(T[] items)
{
    for (int start = 0, end = items.Length - 1; start < end; start++, end--)
    {
        Swap(items, start, end);
    }
} 

private static void Swap<T>(T[] items, int a, int b)
{
    var help = items[a];
    items[a] = items[b];
    items[b] = help;
}

Sometimes I wonder why code looks more difficult than it should be. 有时我想知道为什么代码看起来比应该的难。 Try ` 试试`

string[] words = new string[] {"reverse", "this", "string", "type", "array"};
string[] reverse = new string[words.Length];
int start = 0;`

for(int i = words.Length - 1; i >= 0; i--){
    reverse[start] = words[i];
    s++;
}

foreach(string s in reverse){
    Console.Write(s + ", ");
}

Hope this helps =) or use another for loop inside the for loop counting up instead of using start. 希望这有帮助=)或在for循环内使用另一个for循环计数而不是使用start。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM