将[,]元素引用为[]? [英] Referencing [,] element as [ ]?

查看:70
本文介绍了将[,]元素引用为[]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个6 x 7元素的二维数组。我希望能够按顺序引用元素,就好像行是端对端放置一样。我知道Array.Length属性会给我数组的长度为42,

但是我希望能够引用特定的元素。例如,我希望
能够像foo [24]一样引用foo [3,2]。


有什么方法可以在C#本地执行此操作?现在我正在使用转换

函数,它接受一个索引并返回一个单元格引用。如果可以的话,我想

消除这个功能。在此先感谢。


-

David Veeneman

远见系统

I have a two-dimensional array with 6 x 7 elements. I''d like to be able to
reference the elements sequentially, as if the rows were laid end-to-end. I
know the Array.Length property will give me the length of the array as 42,
but I''d like to be able to reference specific elements. For example, I''d
like to be able to reference foo[3,2] as foo[24].

Is there any way to do this natively in C#? Right now I''m using a conversion
function that takes an index and returns a cell reference. I''d like to
eliminate the function, if I can. Thanks in advance.

--
David Veeneman
Foresight Systems

推荐答案

这样的事情?


foo [Math.Floor(24/7),24%7] // foo [24]


在C中你可以满足于你所说的,但它们只是彻头彻尾的令人讨厌的记忆

超支恰好工作......


David Veeneman <哒**** @ nospam.com>在留言中写道

新闻:%2 *************** @ TK2MSFTNGP12.phx.gbl ...
Something like this?

foo[Math.Floor(24/7),24%7] // foo[24]

In C you could fo what you say, but they were just downright nasty memory
overruns that happened to work...

"David Veeneman" <da****@nospam.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
我有一个具有6 x 7个元素的二维阵列。我希望能够按顺序引用元素,就像行是端对端放置一样。我知道Array.Length属性会给我数组的长度为42,
但是我希望能够引用特定的元素。例如,我希望能够引用foo [3,2]作为foo [24]。

有没有办法在C#中本地执行此操作?现在我正在使用
转换函数,它接受一个索引并返回一个单元格引用。如果可以的话,我会想要消除这个功能。在此先感谢。

-
David Veeneman
预见系统
I have a two-dimensional array with 6 x 7 elements. I''d like to be able to
reference the elements sequentially, as if the rows were laid end-to-end. I
know the Array.Length property will give me the length of the array as 42,
but I''d like to be able to reference specific elements. For example, I''d
like to be able to reference foo[3,2] as foo[24].

Is there any way to do this natively in C#? Right now I''m using a
conversion function that takes an index and returns a cell reference. I''d
like to eliminate the function, if I can. Thanks in advance.

--
David Veeneman
Foresight Systems



David Veeneman写道:
David Veeneman wrote:
我有一个带有6 x 7个元素的二维数组。我希望能够按顺序引用元素,就像行是端对端放置一样。我知道Array.Length属性会给我数组的长度为42,
但是我希望能够引用特定的元素。例如,我希望能够引用foo [3,2]作为foo [24]。

有没有办法在C#中本地执行此操作?现在我正在使用一个转换
函数,它接受一个索引并返回一个单元格引用。如果可以的话,我想要消除这个功能。提前谢谢。
I have a two-dimensional array with 6 x 7 elements. I''d like to be able to
reference the elements sequentially, as if the rows were laid end-to-end. I
know the Array.Length property will give me the length of the array as 42,
but I''d like to be able to reference specific elements. For example, I''d
like to be able to reference foo[3,2] as foo[24].

Is there any way to do this natively in C#? Right now I''m using a conversion
function that takes an index and returns a cell reference. I''d like to
eliminate the function, if I can. Thanks in advance.




我不这么认为。你最好的策略可能是将你的数组分配为43元素数组。这样,需要处理行的代码

和列可以调用你的产品和求和方法,而

的代码可以处理向量直接到向量。


-

< http://www.midnightbeach.com>



I don''t think so. Your best strategy may be to allocate your array as
a 43 element array. That way, the code that needs to deal with rows
and columns can call your product and sum methods, while the code that
can deal with vectors goes straight to the vector.

--
<http://www.midnightbeach.com>


David Veeneman写道:
David Veeneman wrote:
我有一个带有6 x 7元素的二维数组。我希望能够按顺序引用元素,就像行是端对端放置一样。我知道Array.Length属性会给我数组的长度为42,
但是我希望能够引用特定的元素。例如,我希望能够引用foo [3,2]作为foo [24]。

有没有办法在C#中本地执行此操作?现在我正在使用一个转换
函数,它接受一个索引并返回一个单元格引用。如果可以的话,我想要消除这个功能。在此先感谢。
I have a two-dimensional array with 6 x 7 elements. I''d like to be able to
reference the elements sequentially, as if the rows were laid end-to-end. I
know the Array.Length property will give me the length of the array as 42,
but I''d like to be able to reference specific elements. For example, I''d
like to be able to reference foo[3,2] as foo[24].

Is there any way to do this natively in C#? Right now I''m using a conversion
function that takes an index and returns a cell reference. I''d like to
eliminate the function, if I can. Thanks in advance.




以防万一其他人正在调查这一行调查 - 使用

数组的IList索引器实现没有''工作。例如:


使用System;

使用System.Collections;


class Test

{

static void Main()

{

string [,] array = new string [10,20];

array [0,5] =" Hello";

array [3,4] =" There";


IList list =(IList)数组;


Console.WriteLine(list [5]);

Console.WriteLine(list [64]);

}

}


编译但得到以下结果:

未处理的异常:System.ArgumentException:数组不是一个一维的数组。

在System.Array.GetValue(Int32索引)

在System.Array.System。 Collections.IList.get_Item(Int 32 index)

在Test.Main()


(使用2.0和泛型无论如何都没有帮助我可以看到。)


Jon



Just in case anyone else was investigating this line of enquiry - using
the IList indexer implementation for arrays doesn''t work. For instance:

using System;
using System.Collections;

class Test
{
static void Main()
{
string[,] array = new string[10,20];
array[0,5] = "Hello";
array[3,4] = "There";

IList list = (IList)array;

Console.WriteLine (list[5]);
Console.WriteLine (list[64]);
}
}

compiles but gives the following result:
Unhandled Exception: System.ArgumentException: Array was not a
one-dimensional array.
at System.Array.GetValue(Int32 index)
at System.Array.System.Collections.IList.get_Item(Int 32 index)
at Test.Main()

(Using 2.0 and generics doesn''t help either as far as I can see.)

Jon


这篇关于将[,]元素引用为[]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆