交换二维数组中的元素 C#

发布于 2024-10-24 10:18:49 字数 499 浏览 8 评论 0原文

我正在使用 C#,我对该语言相当陌生,但我之前使用过类似的语言,所以我了解基本语法。

我有一个 Object 类型的二维数组。 (X 代表什么值,Y 代表什么记录)它在第 0 列和第 1 列中存储两个字符串,在第 2 列中存储一个 MessageBoxIcon 和一个 MessageBoxButtons

我希望能够交换两条记录。

每次对数组进行更改时,我都会用第 1 列填充列表框。 (使用循环)我对这个系统很满意。 我已将 + 和 - 按钮放置在列表框的一侧,但我不知道如何执行其后面的代码。

我希望这样当我单击 + 按钮时,它会将当前选定的记录向上增加一条记录。 (IE 它减少了它的 Y 位置并增加了其上方记录的 Y 坐标)它需要增加与该记录关联的所有值。

有人可以为我提供一个函数来执行此操作吗?

我希望我对此解释得足够好。

I am using C#, I'm fairly new to the language but I have used similar languages before so I understand basic syntax.

I have a 2D array of type Object. (X represents what value and Y is what record) It stores two strings in columns 0 and 1 and a MessageBoxIcon in 2 and a MessageBoxButtons in 3.

I would like the ability to swap two records.

I populate a listBox with column 1 every time a change is made to the array. (using a loop) I am happy with this system.
I have placed + and - buttons to the side of the listBox but I cannot figure out how to do the code behind it.

I want it so that when I click the + button it bumps the currently selected record up one record. (I.E. It decreases it's Y location and increase the Y coordinate of the record above it) It would need to bump all the values associated with that record.

Could someone provide me with a function to do this?

I hope I explained this well enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是男神闪亮亮 2024-10-31 10:18:49

这需要以交换两个变量值的旧方式完成:

var t = a;
a = b;
b = t;

但是,由于 a 和 b 是二维数组的行,因此必须一次完成一个元素。

public void Swap2DRows(Array a, int indexOne, int indexTwo) {
  if (a == null} { throw new ArgumentNullException("a"); }
  if (a.Rank != 2) { throw new InvalidOperationException("..."); }

  // Only support zero based:
  if (a.GetLowerBound(0) != 0) { throw new InvalidOperationException("..."); }
  if (a.GetLowerBound(1) != 0) { throw new InvalidOperationException("..."); }

  if (indexOne >= a.GetUpperBound(0)) { throw new InvalidOperationException("..."); }
  if (indexTwo >= a.GetUpperBound(0)) { throw new InvalidOperationException("..."); }

  for (int i = 0; i <= a.GetUpperBound(1); ++i) {
    var t = a[indexOne, i];
    a[indexOne, i] = a[indexTwo, i];
    a[indexTwo, i] = t;
  }
}

这可以推广到处理任意下限。

This will need to be done in the old way for swapping two variable's values:

var t = a;
a = b;
b = t;

But, with a and b being rows of a 2d array this has to be done one element at a time.

public void Swap2DRows(Array a, int indexOne, int indexTwo) {
  if (a == null} { throw new ArgumentNullException("a"); }
  if (a.Rank != 2) { throw new InvalidOperationException("..."); }

  // Only support zero based:
  if (a.GetLowerBound(0) != 0) { throw new InvalidOperationException("..."); }
  if (a.GetLowerBound(1) != 0) { throw new InvalidOperationException("..."); }

  if (indexOne >= a.GetUpperBound(0)) { throw new InvalidOperationException("..."); }
  if (indexTwo >= a.GetUpperBound(0)) { throw new InvalidOperationException("..."); }

  for (int i = 0; i <= a.GetUpperBound(1); ++i) {
    var t = a[indexOne, i];
    a[indexOne, i] = a[indexTwo, i];
    a[indexTwo, i] = t;
  }
}

This could be generalised to handle arbitrary lower bounds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文