简体   繁体   English

如何在C#中不使用System.Collection或LINQ的情况下从数组中删除元素?

[英]How to remove elements from array without using System.Collection or LINQ in C#?

I've got a problem when "removing" elements from array in C#, but without using System.Collection and LINQ . 在C#中从数组“删除”元素但不使用System.Collection和LINQ时遇到问题。

for example: 例如:

I have a Product class which contains fields p_name , p_number and is_useful (this value is true by default); 我有一个Product类,其中包含字段p_namep_numberis_useful (默认情况下为true );

I also have a ShoppingCart class which contains field market_name , products_quantity and Products (the type is Product[] , storing all products) 我还有一个ShoppingCart类,其中包含字段market_nameproducts_quantityProducts (类型为Product[] ,存储所有产品)

Suppose I've created four Product objects p1 , p2 , p3 , p4 , and then created one ShoppingCart object sc with a fixed length Products[4] ( p1 , p2 , p3 & p4 are stored it); 假设我已经创建了四个Product对象p1p2p3p4 ,然后创建了一个ShoppingCart对象sc ,其具有固定长度的Products[4] (存储了p1p2p3p4 );

Then I set the is_useful value of p2 to false , so p2 in the Products[4] will become useless for the ShoppingCart , and it needs to be removed from Products[4] . 然后,我将p2is_useful值设置为false ,因此Products[4]中的p2对于ShoppingCart变得无用,需要将其从Products[4]删除。

is it possible that remove p2 from Products[4] , and the length of this array will be reduced to 3 ( Products[3] with p1 , p3 & p4 inside), and it still belongs to the Shoppingcart object sc (like when I use sc.Products to get the all products, it will refer to the new Products[3] )? 是否有可能从Products[4]中删除p2 ,并且此数组的长度将减少为3(内部有p1p3p4 Products[3] ),它仍然属于Shoppingcart对象sc (例如当我使用sc.Products获得所有产品,它将引用新的Products[3] )吗?

Thanks! 谢谢!

Use Array.Resize static method: 使用Array.Resize静态方法:

  • replace element you'd like to remove with the last one 用最后一个替换您要删除的元素
  • call Array.Resize with your array reference and length set to current length - 1 调用Array.Resize并将数组引用和长度设置为当前长度-1
// let's say you'd like to remove the i-th element
//
// You don't have to move Products[i] to the last position,
// because it will be removed anyway.
sc.Products[i] = sc.Products[sc.Products.Length - 1];
// Call Array.Resize to change array size
Array.Resize(sc.Products, sc.Products.Length - 1);

Here are the way of top of my head: 这是我的头上方法:

  1. setting the element to null (and moving it to end : optional), and handling the null value in you access code. 将元素设置为null(并将其移动到end:可选),并在访问代码中处理null值。 (you can use a wrapper method/class for this too) adv: this doesn't involve in any object recreations, reallocation. (您也可以为此使用包装器方法/类)adv:这不涉及任何对象重新创建,重新分配。

  2. Array.Resize() : involves array reallocation, but if your array is short and the operation is very in-frequent you can live with this solution. Array.Resize() :涉及数组重新分配,但是如果您的数组很短并且该操作很少发生,则可以使用此解决方案。 http://msdn.microsoft.com/en-us/library/bb348051.aspx ) http://msdn.microsoft.com/zh-CN/library/bb348051.aspx

  3. Using Collection: Ideal for dynamic sized lists/arrays. 使用集合:动态大小的列表/数组的理想选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM