如何在C#中将对象存储在二维数组中? [英] How store object in Array two dimensional in C#?

查看:273
本文介绍了如何在C#中将对象存储在二维数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我想存储对象EX:
在[0,0]中,我想存储单词"document"和数字"1".
我要在[0,1]中存储单词"go"和数字"12".
我不能只在数组[0,0]中存储"go"或"12".

请,如果可以,如何访问[0,1]?

Hi!
I want to store Object EX:
In [0,0], I want store word "document" and number "1".
In [0,1],I want store word "go" and number "12".
I can not store in array [0,0] only "go" or "12".

Please If I can ,How can I access to [0,1]?

推荐答案

...编辑#1 ...

如果您确实要存储一个包含int和一个字符串的对,而不是存储原始帖子中显示的两个字符串,这对您在以后与LanFanNinja进行评论交流时会出现,请更改下面的代码以使用Tuple< int,string> ;和做正确的事"来定义两个查找函数:一个返回字符串,另一个返回一个int.

下面显示的代码仅处理一对字符串.

...结束编辑#1 ...

可能会帮助您将Array [#n1,#n2]视为象棋盘:在任何位置,行"索引指定的地址"和列"索引指定的位置:只有一个棋子.

似乎在这里您要在数组的每个位置中存储两个字符串.

我认为,您需要在这里考虑的另一件事是,您将字符串存储为对象",这意味着当您将字符串存储为对象时以及在将它们检索为对象时,它们将被装箱",您必须将它们强制转换回Type String才能将它们用作字符串:这很昂贵,而不是最佳实践".因此,它将无法编译:
... edit #1 ...

If you really want to store a pair consisting of an int and a string, not two strings as shown in your original post, which came out in your later exchange in comments with LanFanNinja, then change the code below to use Tuple<int, string>, and "do the right thing" to define two look-up functions: one that returns a string, and one that returns an int.

The code shown below deals only with a pair of strings.

... end edit #1 ...

It may help you to think of an Array[#n1, #n2] as like a chess-board: at any one location, the "address," specified by the "row" index, and "column" index: there can be only one chess-piece.

It seems here you are trying to store two strings in each location in the Array.

The other thing you need to think about here, in my opinion, is that you are storing strings as ''objects,'' and that means they will be "boxed" when you store them, and when you retrieve them, as objects, you will have to cast them back to Type String to use them as strings: that''s expensive, and not "best practice." So this will not compile:
private object[,] objAry = new object[1,1];

objAry[0, 0] = "string1";
objAry[0, 1] = "string2";

object o1 = objAry[0, 0];

string newString = o1;

您可以通过几种其他方法在.NET中获得所需的内容,包括使用Dictionary,但是让我们在这里使用Tuple对象看一个简单的解决方案:

There are several alternate ways you could go about getting what you want in .NET, including the use of a Dictionary, but let''s look at a simple solution here using the Tuple object:

private Tuple<string, string> tupleEntryPlaceHolder;

private List<Tuple<string, string>> tupleList = new List<Tuple<string, string>>();

如何将带有一对字符串的新元组添加到列表中:

How do you add a new Tuple with your pair of strings to the List:

tupleEntryPlaceHolder = Tuple.Create("document1", "1");

tupleList.Add(tupleEntryPlaceHolder);

如何访问元组列表中特定项目(如项目#0)中的元素:

How do you access the elements inside a specific item, like item #0, in your List of Tuples:

string result1 = tupleList[0].Item1;

string result2 = tupleList[0].Item2;

为使访问更简单,让''为此定义了一个函数:

To make access a little easier, let''s define a function for that:

private string getTuple(int tupleIndex, bool isItem1)
{
   tupleEntryPlaceHolder = tupleList[tupleIndex];

   return isItem1 ? tupleEntryPlaceHolder .Item1 : tupleEntryPlaceHolder .Item2;
}

现在,您可以像这样访问:

So now, you can access like this:

string result1 = getTuple(0, true);

string result2 = getTuple(0, false);

这里您可以快速,高效地使用解决方案,不需要拆箱"即可将存储的字符串恢复为Type String即可使用.

正如我所提到的,这也可以使用.NET中的其他数据结构对象来完成,例如Dictionary .而且,也许其他人会在此线程上响应另一种解决方案.

顺便说一下,Tuple对象有原始"形式,每个Tuple最多可以处理8个项目,您甚至可以通过在Tuple定义内粘贴另一个Tuple来将Tuple扩展到每个Tuple超出8个项目.请参阅:[ ^ ] .. .和:[ ^ ]

...编辑#2 ...


在考虑是否要使用Dictionary或Tuple或类似的方法实现这样的解决方案时要牢记的一个重要注意事项是可能存在重复键值的问题.

对于通用词典,添加具有重复键的项目将导致类型为''ArgumentException的运行时错误.

是的,您可以破解相当于允许重复键的Dictionary的等效方法,如果您在CodeProject上进行搜索,则会发现一些与此相关的讨论/文章,但是,解决方案很复杂,而且通常是有争议的",例如您将看到是否阅读了注释.

在C-SharpCorner,有两个有趣的系列文章,由Vulpes [
...结束编辑#2 ...

Here you have a solution that will be fast, and efficient, to use, will not require "unboxing" to get your stored strings back into Type String to be able to use them as Strings.

As I mentioned, this could also be done using other data-structure objects in .NET, like a Dictionary<string, string>, for example. And, perhaps, someone else, on this thread will respond with another type of solution.

By the way, there are "raw" forms of the Tuple object that can handle up to eight items per Tuple, and you can even extend a Tuple beyond eight items per Tuple by sticking another Tuple inside the Tuple definition. See:[^] ... and:[^]

... edit #2 ...


An important consideration to keep in mind in considering whether to implement a solution like this, with a Dictionary, or a Tuple, or a whatever, is the issue of possible duplicate key values.

For a generic Dictionary, adding an item with a duplicate Key will cause a run-time error of Type ''ArgumentException.

Yes, you can hack the equivalent of a Dictionary that allows duplicate keys, and if you search on CodeProject, you''ll find some discussions/articles on this, but, the solutions are complex, and often they are "controversial," as you''ll see if you read the comments.

At C-SharpCorner is an interesting two-part series, "A Dictionary class which permits duplicate keys" by Vulpes[^],[^], that provides a good overview on the considerations for design of a duplicate Key collection entity, and a very professionally coded and commented source-code example.

For a Tuple, adding an item with the same Key will not cause a compile error, or run-time error, but will simply replace the Value of that one Key item.

And, finally, for the use of the "older" data-structure, the HashTable, you also have error issues of adding duplicate key errors possible, and, the need to cast the results of a look-up back to some "original" Type.

... end edit #2 ...


xxx兄弟,

尝试改用Hashtable对象.

供您参考:
http://www.dotnetperls.com/hashtable [ http://en.wikipedia.org/wiki/Hash_table [
Hi xxxx man,

Try using Hashtable object instead.

For your reference:
http://www.dotnetperls.com/hashtable[^]

http://en.wikipedia.org/wiki/Hash_table[^]

Regards,
Eduard


这篇关于如何在C#中将对象存储在二维数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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