我有一个基于锯齿状数组的循环,我需要在不同的地方使用多次.
如何防止自己一次又一次地重写这个循环,以便我将它复制?
foreach (int[] columns in rowsAndColumns)
{
foreach (int element in columns)
{
}
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个多维数组,并希望将整个内部数组设置为等于单独的(单维)数组.除了遍历数组中的每个位置和设置外,我该怎么做grid[row][val] = inputNums[val]?
int[,] grid = new int[20,20];
// read a row of space-deliminated integers, split it into its components
// then add it to my grid
string rowInput = "";
for (int row = 0; (rowInput = problemInput.ReadLine()) != null; row++) {
int[] inputNums = Array.ConvertAll(rowInput.Split(' '), (value) => Convert.ToInt32(value))
grid.SetValue(inputNums , row); // THIS LINE DOESN'T WORK
}
Run Code Online (Sandbox Code Playgroud)
我得到的具体错误是:
"争议异常处理:数组不是一维数组."
我想在c ++中创建锯齿状的二维数组.
int arrsize[3] = {10, 5, 2};
char** record;
record = (char**)malloc(3);
cout << endl << sizeof(record) << endl;
for (int i = 0; i < 3; i++)
{
record[i] = (char *)malloc(arrsize[i] * sizeof(char *));
cout << endl << sizeof(record[i]) << endl;
}
Run Code Online (Sandbox Code Playgroud)
我想设置record[0]名称(应该有10个字母),record[1]标记(应该有5位数字)和record[3]Id(应该有2位数字).我该如何实现呢?我直接将记录数组写入二进制文件.我不想用struct和class.
我想用c#做一个数独.我应该使用锯齿状或多维数组.为什么?
我知道,对于简单的数组逻辑,例如数独,锯齿状数组更快更有效.但也许还有其他争论?
提前致谢.
编辑:我需要在数独上执行一些解决方法.像回溯算法一样.我发现多维数组有更好的语法.
我想我的问题是:哪个最简单易用,实现和修改?
所以我想创建一个数组数组,从我收集的数据中,我需要一个锯齿状数组.
我有一个数组x(i,j),它定义了一个整数的正方形nxn矩阵.对于k的每次迭代,交换该数组内的两个整数值以尝试并更好地使用矩阵x(i,j).我需要创建一个数组,在每次k次迭代后存储这个矩阵x(i,j).
澄清一下,说我有没有
1 2 3
4 5 6
7 8 9
Run Code Online (Sandbox Code Playgroud)
我执行迭代交换数组中的两个元素:
7 2 3
4 5 6
1 8 9
Run Code Online (Sandbox Code Playgroud)
我希望能够将这些数组存储在数组中,以便随时调用.我试过这里的解决方案:
Dim y() As Variant 'Declare as a variant to contain arrays
ReDim y(1 To IterationLimit) 'This will be the jagged array
For k = 1 To IterationLimit
'Some code goes here for the swap
y(k) = x(i,j)
next k
Run Code Online (Sandbox Code Playgroud)
现在说我想要第85次迭代.我希望能够输入y(85)[或类似]来拉出特定时间的矩阵x(i,j).
希望我已经足够好地解释了这一点.感谢任何帮助,我真的坚持这一点.
编辑:删除的代码
是否有一个潜在的1衬里允许我从2D的内部数组的某个索引创建一个新的1维数组?
示例采用每个内部数组的第一个元素:
double[][] array2D = new double[10][] // with inner arrays say double[5]
double[] array1D = new double[10];
for (int i=0; i<array2D.Length; i++)
{
array1D[i] = array2D[i][0];
}
Run Code Online (Sandbox Code Playgroud) 我打开了CSV文件,但我无法弄清楚如何将结果数组拆分成另一个数组.我目前有以下代码,希望它更能让我了解我的意思:
private void ReadFileToArray(StreamReader file)
{
int i = 0;
string[][] FP_GamesArray;
while (!file.EndOfStream)
{
string line = file.ReadLine();
if (!String.IsNullOrWhiteSpace(line))
{
string[] values = line.Split(',');
MessageBox.Show(values.ToString());
FP_GamesArray[i] = values;
}
i++;
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我得到两个错误:一个说法Cannot implicitly convert type 'string[]' to 'string',第二个说Use of unassigned local variable 'FP_GamesArray'.
如何在锯齿状数组中使用空条件运算符的正确语法?
int[][][] array = new int[5][][];
// fill with data here
bool result = array[0]?.[3]?.[2] ?? == 3;
Run Code Online (Sandbox Code Playgroud)
该参考文献未提供任何有关该信息的信息
我有以下锯齿状阵列-
int[][] triangle =
{
new int[] { 3 },
new int[] { 7,4 },
new int[] { 2,4,6 },
new int[] { 8,5,9,3 }
};
Run Code Online (Sandbox Code Playgroud)
然后我复制了它-
int[][] temporaryTriangle = new int[triangle.Length][];
Array.Copy(triangle, temporaryTriangle, triangle.Length);
Run Code Online (Sandbox Code Playgroud)
我对副本的子数组进行了排序-
for (int i = 0; i < temporaryTriangle.Length; i++)
{
Array.Sort(temporaryTriangle[i]);
}
Run Code Online (Sandbox Code Playgroud)
我发现源数组的子数组也被排序了!我的问题是为什么会发生这种情况,我的意思是为什么当我对副本的子数组进行排序时,数组的子数组会被排序?
用法:
foreach (var subarray in triangle)
Console.WriteLine(string.Join(" ", subarray));
foreach (var subarray in temporaryTriangle)
Console.WriteLine(string.Join(" ", subarray));
// Output:
// 3
// 4 7
// 2 4 6
// 3 5 …Run Code Online (Sandbox Code Playgroud) 您如何看待它们之间有什么区别?
new object[][] { new object[] { "1" }, new object[] { "2" }, new object[] { "3" } }
new object[] { new object[] { "1" }, new object[] { "2" }, new object[] { "3" } }
Run Code Online (Sandbox Code Playgroud)
他们唯一的区别我看到的是重载的方法.
static void Arr(object[][] oa)
static void Arr(object[] oa)
Run Code Online (Sandbox Code Playgroud)
也许记忆的效率?
这是我的锯齿状数组,它在初始化期间工作得很好:
int a[][]=new int[5][];
for(int i=0;i<5;i++)
{
for(int j=0;j<=i;j++)
{
a[i]=new int[i+1];
a[i][j]=j+1;
System.out.print(a[i][j]);
}
System.out.println("");
}
Run Code Online (Sandbox Code Playgroud)
给我想要的输出:
1
12
123
1234
12345
Run Code Online (Sandbox Code Playgroud)
但是使用循环打印相同的数组只会产生荒谬的输出:
for(int i=0;i<5;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print(a[i][j]);
}
System.out.println("");
}
Run Code Online (Sandbox Code Playgroud)
得到:
1
02
003
0004
00005
Run Code Online (Sandbox Code Playgroud)
我无法得出任何结论.代码有什么问题?