在C#中如何以动态方式添加数据并快速访问和删除数据元素? [英] In C# How to add data in dynamic way and access and remove data element fast?
问题描述
在C#中我为动态数据定义了数据类型(数组,数组列表,集合列表,字典键,哈希集)
*哈希集:缓慢添加数据,快速删除数据并快速访问数据元素
*字典键:缓慢添加数据,快速删除数据并快速访问数据元素
*数组list:快速添加数据,慢慢删除数据和访问数据元素非常慢(你必须循环foreach你想要的元素)
*集合列表:快速添加数据,慢慢删除数据并缓慢访问数据元素(你必须循环foreach你想要的元素)
*二维数组:快速添加数据和访问数据元素但不是删除数据而不是动态添加数据。
我建议你将.NET的数组视为两个不同的对象类:一维数组(向量,等级) = 1)和多维数组(秩> 1)。一旦宣布,它们的长度是不变的。你只能通过创建一个没有你想要删除的项目的新数组来删除项目。
一般来说,依赖于重新设置的代码是错误的。调整多维数组的大小。
如果你必须能够从多维数组中删除/删除项目,那么你需要使用锯齿状数组,一个数组数组,其中每个Array项可以有不同的长度,甚至有不同的维数:[ ^ ]。
这是使用Linq删除第二个锯齿数组数组中第三项的示例: pre lang =cs> int [] [] jAry = new int [] []
{
new int [] { 1 , 2 , 3 , 4 , 5 },
new int [] { 0 , 2 , 4 , 6 },
new int [ ] { 1 , 3 , 5 , 7 , 9 , 11 }
};
// 需要声明:
// 使用System.Linq;
// 删除第3名。第二个数组中的元素
jAry [ 1 ] = jAry [ 1 ]。其中((itm,ndx)= > ndx!= 2 )。ToArray();这使用'Where Linq运算符,它可以选择允许你使用你枚举的数组的索引。
到这个时候,我希望你想知道为什么你必须在必须删除元素的情况下使用数组:因为我认为你应该忘记这个想法并使用(鼓点,夸耀)一个通用列表!
对于2d-Array的等价物,列表列表:List< list>< int>> List2d = new 列表< list>< int>>
{
new 列表< int> { 1 , 2 , 3 , 4 , 5 },
new 列表< int> { 0 , 2 , 4 , 6 },
new 列表< int> { 1 , 3 , 5 , 7 , 9 , 11 }
};
// Linq不需要
// 删除第3个。第二个数组中的元素
List2d [ 1 ]。RemoveAt( 2 ); < / int > < / int > < / int > < / int > < / 列表 > < / int > < / list >学习,实验,观察, nalyze,然后......你决定。
因为每个硬币都有两面,所以一切都有利弊。 :笑:
看看这个答案: http:/ /stackoverflow.com/questions/576176/net-collection-for-fast-insert-delete [ ^ ]
-KR
In C# I defined data type (array, array list, collection list, dictionar key , hashset) for dynamic data
* Hash set: add data slowly, remove data fast and access data element fast
* Dictionary key : add data slowly, remove data fast and access data element fast
* array list: add data fast, remove data slowly and access data element very slow (you must loop foreach for which you want element)
* collection list : add data fast, remove data slowly and access data element slow (you must loop foreach for which you want element)
*two dimession array: add data and access data element very fast but not to remove data and not to add data dynamically.
I suggest you think of .NET's Arrays as two distinct classes of object: one-dimensional Arrays (vectors, rank = 1), and multi-dimensional Arrays (rank > 1). Their length is immutable once declared. You can only remove items by creating a new Array without the items you wish to be removed.
In general it is a mistake to ever have code that relies on re-sizing a more than one-dimension Array.
If you must have the ability to delete/remove items from a multi-dimensional Array, then you need to use a "jagged" Array, an Array of Arrays, where each Array item can have a different length, and even have different numbers of dimensions: [^].
Here's an example of using Linq to remove the third item in the second Array of a jagged Array:int[][] jAry = new int[][] { new int[] { 1, 2, 3, 4, 5 }, new int[] { 0, 2, 4, 6 }, new int[] { 1, 3, 5, 7, 9, 11 } }; // requires declaring: // using System.Linq; // remove 3rd. element in second array jAry[1] = jAry[1].Where((itm, ndx) => ndx != 2).ToArray();This uses the 'Where Linq operator that optionally allows you to use the index of the Array you are enumerating.
By this time, I hope you are wondering why you would ever want to use Arrays in the case where you must remove elements: because I think you should forget that idea and use (drum-roll, fanfare) a generic List !
For an equivalent to a 2d-Array, a List of Lists:List<list><int>> List2d = new List<list><int>> { new List<int>{ 1, 2, 3, 4, 5 }, new List<int>{ 0, 2, 4, 6 }, new List<int>{ 1, 3, 5, 7, 9, 11 } }; // Linq not required // remove 3rd. element in second array List2d[1].RemoveAt(2);</int></int></int></int></list></int></list>Study, experiment, observe, analyze, then ... you decide.
As every coin has two sides, there'd always be pros/cons for everything. :laugh:
Look at this answer though: http://stackoverflow.com/questions/576176/net-collection-for-fast-insert-delete[^]
-KR
这篇关于在C#中如何以动态方式添加数据并快速访问和删除数据元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!