C#:字符数组与2方法相反,一种方法与直接长度相反。一种有价值的方法。为什么第二种方法没有';不工作?

C#:字符数组与2方法相反,一种方法与直接长度相反。一种有价值的方法。为什么第二种方法没有';不工作?,c#,arrays,char,reverse,C#,Arrays,Char,Reverse,我创建了两个方法来反转字符数组,当我给一个临时字符数组指定长度,然后执行反转时,它是有效的(methode:reverseChar:“char[]tempChar=new char[testChar.length];”),但是当我给临时字符数组赋值,然后执行反转时,它不起作用(methode:reversCharVersion2:“char[]tempChar=testChar;”)。谁能看看这个问题并帮我找到原因,非常感谢 using System; using System.Collecti

我创建了两个方法来反转字符数组,当我给一个临时字符数组指定长度,然后执行反转时,它是有效的(methode:reverseChar:“char[]tempChar=new char[testChar.length];”),但是当我给临时字符数组赋值,然后执行反转时,它不起作用(methode:reversCharVersion2:“char[]tempChar=testChar;”)。谁能看看这个问题并帮我找到原因,非常感谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class Test
    {
        public char[] reversChar(char[] testChar)
        {

            char[] tempChar = new Char[testChar.Length];
            for (int i = 0; i < testChar.Length; i++)
            {
                tempChar[i] = testChar[(testChar.Length - 1) - i];
            }
            return tempChar;
        }

        public char[] reversCharVersion2(char[] testChar)
        {
            char[] tempChar = testChar;
            for (int i = 0; i < testChar.Length; i++)
            {
                tempChar[i] = testChar[(testChar.Length - 1) - i];
            }
            return tempChar;
        }

        static void Main(string[] args)
        {
            //Vorbereitung Test Data
            Test myTest = new Test();
            char[] testChar = { '1', '2', '3', '4', '5' };
            char[] outputChar;

            //Methode 1 funktioniert
            outputChar = myTest.reversChar(testChar);
            Console.WriteLine(outputChar);

            //Methode 2 funktioniert nicht
            outputChar = myTest.reversCharVersion2(testChar);
            Console.WriteLine(outputChar);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间测试
{
课堂测试
{
公共字符[]反向字符(字符[]测试字符)
{
char[]tempChar=新字符[testChar.Length];
for(int i=0;i
第一个不是反转。它只是将原始字符数组按相反顺序复制到新数组

但在第二种情况下,您使用的是相同的数组,它们的值在 相同的参考位置。在第二种情况下得到的输出是54345 在第二种情况下,您需要使用以下代码

public char[] reversCharVersion2(char[] testChar)
{
     char[] tempChar = testChar;
     char temp;
     for (int i = 0; i < (testChar.Length/2); i++)
     {
         temp = tempChar[i];
         tempChar[i] = testChar[(testChar.Length - 1) - i];
         testChar[(testChar.Length - 1) - i] = temp;
     }
     return tempChar;
}
public char[]reversion2(char[]testChar)
{
char[]tempChar=testChar;
焦炭温度;
对于(int i=0;i<(testChar.Length/2);i++)
{
temp=tempChar[i];
tempChar[i]=testChar[(testChar.Length-1)-i];
testChar[(testChar.Length-1)-i]=温度;
}
返回tempChar;
}

第一个不是反转。它只是将原始字符数组按相反顺序复制到新数组

但在第二种情况下,您使用的是相同的数组,它们的值在 相同的参考位置。在第二种情况下得到的输出是54345 在第二种情况下,您需要使用以下代码

public char[] reversCharVersion2(char[] testChar)
{
     char[] tempChar = testChar;
     char temp;
     for (int i = 0; i < (testChar.Length/2); i++)
     {
         temp = tempChar[i];
         tempChar[i] = testChar[(testChar.Length - 1) - i];
         testChar[(testChar.Length - 1) - i] = temp;
     }
     return tempChar;
}
public char[]reversion2(char[]testChar)
{
char[]tempChar=testChar;
焦炭温度;
对于(int i=0;i<(testChar.Length/2);i++)
{
temp=tempChar[i];
tempChar[i]=testChar[(testChar.Length-1)-i];
testChar[(testChar.Length-1)-i]=温度;
}
返回tempChar;
}

数组是引用。将一个数组变量指定给另一个数组变量时,不会创建副本,但两个变量都指向同一个数组。由于您的算法只从一个方向遍历数组,因此它将在到达数组之前覆盖数组的另一半。你必须改变你的算法,不是在循环中复制一个字符,而是只在数组的一半上循环,而是在两个方向上交换字符。你的两个变量在第二个例子中指的是同一个数组谢谢你的解释,我改变了我的算法并做了两个方面的工作。现在我明白了单变量和群变量是不同的。使用一组变量,如数组、类名及其所有引用。数组是引用。将一个数组变量指定给另一个数组变量时,不会创建副本,但两个变量都指向同一个数组。由于您的算法只从一个方向遍历数组,因此它将在到达数组之前覆盖数组的另一半。你必须改变你的算法,不是在循环中复制一个字符,而是只在数组的一半上循环,而是在两个方向上交换字符。你的两个变量在第二个例子中指的是同一个数组谢谢你的解释,我改变了我的算法并做了两个方面的工作。现在我明白了单变量和群变量是不同的。有一组变量,比如数组,类名,它的所有引用。谢谢你的回答,非常有用,我的问题已经解决了。谢谢你的回答,非常有用,我的问题已经解决了。