现在,我有两个char
数组,foo1[]
和foo2[]
。当我将它们转换为字符串并输出到控制台时,它们都显示为bar
。我知道我可以像这样做:
int g;
for (int i=0;i<length.foo1;i++) { // loop from 0 to length of array
if (foo1[i]=foo2[i]) { // if foo1[0] and foo2[0] are the same char
g++; // increment a counter by 1
}
else // otherwise...
{
i=100; // set i to something that will halt the loop
}
if (g = length.foo1) { // if the incremented counter = length of array
return true; // arrays are equal, since g only increments
} // in the case of a match, g can ONLY equal the
else { // array length if ALL chars match
return false; // and if not true, false.
要逐个字符比较它们,但我猜想有更简单的方法。有趣的是,我大部分搜索“在c#中比较char[]相等”的结果都是关于比较STRING数组,或者在某些情况下比较字符串或字符数组的一部分是否匹配...我找不到任何关于测试char数组是否相等的简便方法。逐个遍历每个数组是最好的方法吗?还是有某种方法可以比较foo1=foo2或foo1==foo2?对我来说,两种方法都不起作用。
基本上,我需要测试
char foo1[]
和char foo2[]
是否都为{B,A,R}
。
<>
是无效的,而且你试图增加一个未初始化的变量。噢,然后你的if
语句没有布尔条件。这段代码真正要做什么并不是很清楚... - Jon Skeet