反转数组会导致两个数组反转

编程入门 行业动态 更新时间:2024-10-24 17:32:49
本文介绍了反转数组会导致两个数组反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我做了一个简单的程序,它使用一个字符数组来比较一个字符串,看它是否是一个回文,但当我使用Array.Reverse方法来反转第二个数组时,它们最终都会反转。 />

I've made a simple program that uses a character array to compare a string to see if it's a palindrome or not but when I use the Array.Reverse method to reverse the second array both of them end up reversing.

Console.Write("> Enter a word or sentence\n> "); input = Console.ReadLine(); input = input.Replace(" ",""); charArray = input.ToCharArray(); reversedCharArray = charArray; Array.Reverse(reversedCharArray);

我通过使用 修复了问题

I've fixed the problem by using

reversedCharArray = charArray.Reverse().ToArray();

但我很好奇为什么它不起作用方式。 我面临的另一个问题是

But I'm curious as to why it didn't work the other way. The other problem that I'm facing is that

if(charArray == reversedCharArray)

即使阵列相同,也始终返回false(我在输出期间检查了数组的值)我可以看到这样做的另一种方法是输出到一个字符串并比较它们但这似乎是一个不必要的步骤...

Is always returning false even when the arrays are the same (I've checked the values of the arrays during output) The other way I can see to do this is to output to a string and compare them but that seems like an unnecessary step...

推荐答案

查看 reversedCharArray = charArray 的行。您没有通过执行此行来复制数组。你所做的是第二个变量指向与charArray相同的数组实例。您现在有两个指向完全相同的数组实例的变量,charArray和reversedCharArray。你使用一个变量做的事情会被另一个变量反映出来。 至于另一个问题,你不能比较那样的两个数组。您正在比较数组的引用。你所说的是这两个地址是一样的吗?如果你想比较它们而不是迭代遍历数组的内容,逐个元素,你可以将反转的数组转换回字符串,然后比较字符串。 这是一个反转字符串的小函数: Look at the line that says reversedCharArray = charArray. You did not make a copy of the array by executing this line. What you did was a second variable point to the same array instance as charArray. You now have two variables pointing to the exact same array instance, charArray and reversedCharArray. What you do using one variable will be reflected by the other. As for the other question, you cannot compare two arrays like that. You're comparing the references of the arrays. What you're saying is "are these two addresses the same?" If you want to compare them without iterating over the contents of the arrays, element by element, you can convert the reversed array back to a string, then compare the strings. Here's a little function to reverse a string: Function ReverseString(target As String) As String Dim chars() As Char = target.ToCharArray() Array.Reverse(chars) Return New String(chars) End Function

您对 reversedCharArray 的分配创建了一个po inter到 charArray 。在下面的示例中,我使用 CopyTo 方法创建 charArray 的副本。 Your assignment to reversedCharArray created a pointer to charArray. In the example below, I used the CopyTo method to create a copy of the charArray. string input; char [] charArray; char [] reversedCharArray; Console.Write("> Enter a word or sentence\n> "); input = Console.ReadLine(); input = input.Replace(" ", ""); charArray = input.ToCharArray(); reversedCharArray = new char[charArray.GetLength(0)]; charArray.CopyTo(reversedCharArray,0); Array.Reverse(reversedCharArray); Console.WriteLine(charArray); Console.WriteLine(reversedCharArray);

更多推荐

反转数组会导致两个数组反转

本文发布于:2023-06-04 08:35:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/493360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!