c# - 将一维数组的索引转换为二维数组 i。 e.行和列

标签 c# arrays winforms

我有一个 WinForms 应用程序,我在列表框中插入名称和价格..名称和价格分别存储在二维数组中。现在,当我从 listbox 中选择一条记录时,它只给我一个索引,我可以从中获取字符串名称和价格以更新该记录我必须为此更改该索引的名称和价格我想要更新二维数组名称和价格。但所选索引只是一维的。我想将该索引转换为行和列。如何做到这一点?

但是我在这样的列表框中插入记录。

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);
    }
}

最佳答案

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

从二维到一维:

index = x + (y * width)

index = y + (x * height)

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

从一维到二维:

x = index % width
y = index / width 

x = index / height
y = index % height

关于c# - 将一维数组的索引转换为二维数组 i。 e.行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16790584/

相关文章:

c# - 编译器错误 C#

python - 剪切一些值为 255 的行和列

C、 Camel 文转蛇文

winforms - 如何在 PowerShell 中访问 GUI 事件的源?

c# - 当 RenderMode 为 "System"时,ToolStripMenuItem 无法显示复选标记和图像(图标)?

c# - C++ -> C# 使用 SWIG : how to use default constructor for a class

c# - linq查询从多个表中获取计数

c# - .net 核心 3.1 : 'IAsyncEnumerable<string>' does not contain a definition for 'GetAwaiter'

c - 如何检查String是否由C中的数字或字母组成

wpf - 元素主机 : Images in XAML Ribbon Causing Issues