Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将一维数组的索引转换为二维数组i。E行和列_C#_Arrays_Winforms - Fatal编程技术网

C# 将一维数组的索引转换为二维数组i。E行和列

C# 将一维数组的索引转换为二维数组i。E行和列,c#,arrays,winforms,C#,Arrays,Winforms,我有一个应用程序WinForms,在列表框中插入name和price.。name和price分别存储在二维数组中。现在,当我从列表框中选择一条记录时,它只给我一个索引,我可以从中获取字符串名称和价格来更新该记录,我必须在该索引处更改名称和价格,为此,我要更新二维数组名称和价格。但选定的索引只是一维的。我想把索引转换成行和列。怎么做 但我像这样在列表框中插入记录 int row = 6, column = 10; for(int i=0;i<row;i++) { for(int j=

我有一个应用程序
WinForms
,在列表框中插入name和price.。name和price分别存储在二维数组中。现在,当我从
列表框
中选择一条记录时,它只给我一个索引,我可以从中获取字符串名称和价格来更新该记录,我必须在该索引处更改名称和价格,为此,我要更新二维数组名称和价格。但选定的索引只是一维的。我想把索引转换成行和列。怎么做

但我像这样在列表框中插入记录

int row = 6, column = 10;
for(int i=0;i<row;i++)
{
    for(int j=0;j<column;j++)
    {
        value= row+" \t "+ column +" \t "+ name[i, j]+" \t " +price[i, j];
        listbox.items.add(value);
    }
}
int行=6,列=10;
对于(int i=0;i试试这个

int i = OneDimensionIndex%NbColumn
int j = OneDimensionIndex/NbRow //Care here you have to take the integer part

好的,如果我理解正确的话,在你的例子中,
ListBox
条目的数组条目的索引显然是
ListBox
中的索引。然后名称和价格位于该数组元素的索引
0
和索引
1

例如:

string[][] namesAndPrices = ...;

// To fill the list with entries like "Name: 123.45"
foreach (string[] nameAndPrice in namesAndPrices)
   listBox1.Items.Add(String.Format("{0}: {1}", nameAndPrice[0], nameAndPrice[1]));

// To get the array and the name and price, it's enough to use the index
string[] selectedArray = namesAndPrices[listBox1.SelectedIndex];
string theName = selectedArray[0];
string thePrice = selectedArray[1];
如果您有这样一个数组:

string[] namesAndPrices = new string[] { "Hello", "123", "World", "234" };
情况不同,在这种情况下,指数是

int indexOfName = listBox1.SelectedIndex * 2;
int indexOfPrice = listBox1.SelectedIndex * 2 + 1;

虽然我没有完全理解确切的场景,但在一维和二维坐标之间转换的常用方法是:

从二维到一维:

index = x + (y * width)

这取决于您是从左到右还是从上到下阅读

从一维到二维:

x = index % width
y = index / width 


用于将一维索引转换为三维索引或从三维索引转换为一维索引:

(int, int, int) OneToThree(i, dx, dy int) {
    z = i / (dx * dy)
    i = i % (dx * dy)
    y = i / dx
    x = i % dx
    return x, y, z
}

int ThreeToOne(x, y, z, dx, dy int) {
    return x + y*dx + z*dx*dy
}

您可能应该发布一些代码……如果源数组包含像
name price name price…
这样的序列,这是正确的。但是,这不是一个真正的二维数组。将问题更改为添加源代码的相关部分如何?这不是要在注释中发布的内容。如何为多维数组执行此操作阵列?
x = index / height
y = index % height
(int, int, int) OneToThree(i, dx, dy int) {
    z = i / (dx * dy)
    i = i % (dx * dy)
    y = i / dx
    x = i % dx
    return x, y, z
}

int ThreeToOne(x, y, z, dx, dy int) {
    return x + y*dx + z*dx*dy
}