C#,一维包装数组
我有一个一维数组,我需要根据每个单元格的相邻单元格对其运行操作C#,一维包装数组,c#,arrays,C#,Arrays,我有一个一维数组,我需要根据每个单元格的相邻单元格对其运行操作 int length = numberArray.Length; for (int i = 0; i < length; ++i) { int iMinus = (((i - 1) % length) + length) % length; int iPlus = (((i + 1) % length) + length) % length; }` 例如: int length = numberArray.
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
例如:
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
要在第一个单元格上运行操作,我需要访问最后一个单元格和第二个单元格
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
第二个单元格,我需要访问第一个单元格和第三个单元格
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
最后一个单元格,我需要访问第一个单元格和最后一个单元格之前的单元格
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
到目前为止,我的代码是:
public static int[] firstRule(int[] numberArray)
{
for (int i = 0; i < numberArray.Length; i++)
{
if (numberArray[numberArray.Length - 1 - i] == numberArray[i] + 1
&& numberArray[i + 1] == numberArray[i] + 1)
{
numberArray[i] = numberArray[i] - 1;
}
}
return numberArray;
}
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
公共静态int[]firstRule(int[]numberraray)
{
for(int i=0;i
但我的方法的问题是,它只适用于第一个单元,因为它会正确地使用最后一个单元和第二个单元,但在那之后,一切都会崩溃。我不喜欢没有很多代码的帖子,但我不知道如何跟进
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
我正在努力实现以下目标:
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
这些值是从0到3的数字。如果前面的单元格和后面的单元格都是相同的数字,我想将其更改为x+1
例如:假设我有10012。我想将0更改为1。由于相邻单元格都是0。只需保持简单,并使用变量计算左右单元格索引即可。在
for
循环中,您可以执行以下操作
var leftCell = i - 1;
if (leftCell < 0)
{
leftCell = numberArray.Length - 1; // Wrap around to the end...
}
var rightCell = i + 1;
if (rightCell > numberArray.Length - 1)
{
rightCell = 0; // Wrap back around to the beginning...
}
// Now you can update your original code to use these computed indices...
if (numberArray[leftCell] == numberArray[i] + 1
&& numberArray[rightCell] == numberArray[i] + 1)
{
numberArray[i] = numberArray[i] - 1;
}
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
var leftCell=i-1;
if(leftCell<0)
{
leftCell=numberArray.Length-1;//环绕到末尾。。。
}
var-rightCell=i+1;
if(rightCell>numberArray.Length-1)
{
rightCell=0;//回到开头。。。
}
//现在您可以更新原始代码以使用这些计算索引。。。
如果(numberArray[leftCell]==numberArray[i]+1
&&numberArray[rightCell]==numberArray[i]+1)
{
numberArray[i]=numberArray[i]-1;
}
保持简单,使用变量计算左右单元格索引即可。在for
循环中,您可以执行以下操作
var leftCell = i - 1;
if (leftCell < 0)
{
leftCell = numberArray.Length - 1; // Wrap around to the end...
}
var rightCell = i + 1;
if (rightCell > numberArray.Length - 1)
{
rightCell = 0; // Wrap back around to the beginning...
}
// Now you can update your original code to use these computed indices...
if (numberArray[leftCell] == numberArray[i] + 1
&& numberArray[rightCell] == numberArray[i] + 1)
{
numberArray[i] = numberArray[i] - 1;
}
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
var leftCell=i-1;
if(leftCell<0)
{
leftCell=numberArray.Length-1;//环绕到末尾。。。
}
var-rightCell=i+1;
if(rightCell>numberArray.Length-1)
{
rightCell=0;//回到开头。。。
}
//现在您可以更新原始代码以使用这些计算索引。。。
如果(numberArray[leftCell]==numberArray[i]+1
&&numberArray[rightCell]==numberArray[i]+1)
{
numberArray[i]=numberArray[i]-1;
}
试试这个:
var len = numberArray.Length;
for (int i = 0; i < len; i++)
{
var leftIndex = (i - 1 + len) % len;
var rightIndex = (i + 1) % len;
// do your stuff with numberArray[leftIndex] and numberArray[rightIndex]
}
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
var len=numberArray.Length;
对于(int i=0;i
%
是<代码>%len允许您保持在0..len-1范围内,因此您可以像遍历数组一样遍历它,就像它已成为“循环的”尝试以下方法:
var len = numberArray.Length;
for (int i = 0; i < len; i++)
{
var leftIndex = (i - 1 + len) % len;
var rightIndex = (i + 1) % len;
// do your stuff with numberArray[leftIndex] and numberArray[rightIndex]
}
public static void firstRule(int[] numberArray)
{
for (int i = 0; i < numberArray.Length; i++)
{
int? prevElement = i == 0
? numberArray[numberArray.Length-1]
: numberArray[i - 1];
int? nextElement = i == numberArray.Length -1
? numberArray[0]
: numberArray[i + 1];
Console.WriteLine(
String.Format("Prev: {0}; Current: {1}; Next: {2}",
prevElement,
numberArray[i],
nextElement)
);
}
}
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
var len=numberArray.Length;
对于(int i=0;i
%
是<代码>%len允许您保持在0..len-1范围内,所以您可以遍历数组,就好像它已成为“循环的”类似的操作应该可以正常工作。它为每个循环中的操作确定适当的单元格并执行该操作。您没有说明该操作是什么,因此需要填写DoYourOperation方法
public static void firstRule(int[] numberArray)
{
for (int i = 0; i < numberArray.Length; i++)
{
int? prevElement = i == 0
? numberArray[numberArray.Length-1]
: numberArray[i - 1];
int? nextElement = i == numberArray.Length -1
? numberArray[0]
: numberArray[i + 1];
Console.WriteLine(
String.Format("Prev: {0}; Current: {1}; Next: {2}",
prevElement,
numberArray[i],
nextElement)
);
}
}
public static int[] processArray(int[] numberArray)
{
for (int i= 0; i < numberArray.Length; i++)
{
int firstCell;
int secondCell;
//Check if first cell
if(i == 0)
{
firstCell = numberArray[numberArray.length-1]; //Last cell
secondCell = numberArray[i++]; //Next cell
}
//Check if last cell
else if(i == numberArray.Length - 1)
{
firstCell = numberArray[i--]; //Cell before last one
secondCell = numberArray[0]; //First cell
}
else
{
firstCell = numberArray[i--];
secondCell = numberArray[i++];
}
DoYourOperation(firstCell, secondCell);
}
}
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
公共静态int[]processArray(int[]numberArray)
{
for(int i=0;i//位于最后一个单元格之前的单元格
secondCell=numberArray[0];//第一个单元格
}
其他的
{
firstCell=numberArray[i--];
secondCell=numberArray[i++];
}
DoYourOperation(第一个单元格,第二个单元格);
}
}
像这样的东西应该可以用。它为每个循环中的操作确定适当的单元格并执行该操作。您没有说明该操作是什么,因此需要填写DoYourOperation方法
public static int[] processArray(int[] numberArray)
{
for (int i= 0; i < numberArray.Length; i++)
{
int firstCell;
int secondCell;
//Check if first cell
if(i == 0)
{
firstCell = numberArray[numberArray.length-1]; //Last cell
secondCell = numberArray[i++]; //Next cell
}
//Check if last cell
else if(i == numberArray.Length - 1)
{
firstCell = numberArray[i--]; //Cell before last one
secondCell = numberArray[0]; //First cell
}
else
{
firstCell = numberArray[i--];
secondCell = numberArray[i++];
}
DoYourOperation(firstCell, secondCell);
}
}
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
公共静态int[]processArray(int[]numberArray)
{
for(int i=0;i//位于最后一个单元格之前的单元格
secondCell=numberArray[0];//第一个单元格
}
其他的
{
firstCell=numberArray[i--];
secondCell=numberArray[i++];
}
DoYourOperation(第一个单元格,第二个单元格);
}
}
来自您的评论
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
这些值是0到3之间的数字。如果前面的单元格和后面的单元格都是相同的数字,我想将其更改为x+1
例如:假设我有10012。我想将0更改为1。因为相邻单元均为0。
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
我将创建一个新数组,用现有数组的值填充它,然后根据现有数组中的值的结果更改新数组的值
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
当Op获取错误值时进行编辑
我怀疑您可能没有正确复制阵列:
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`