字符串数组中的 NULL
如何删除字符串数组中的空值,
例如 { ,-2,3, ,-4,+5, ,66...}
我需要删除中间的那些空值并重新调整数组的大小
我不'不想使用列表
我不想创建新数组
如果需要,请告诉我用简单的代码就可以实现。 谢谢。
How to remove Null values in string array
Like { ,-2,3, ,-4,+5, ,66...}
I need to remove those null values in between and re-size the array
I don't want to use lists
I don't want to create a new array
Please let me know if it is possible with simple code.
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,如果不创建新数组,这是不可能的。您无法调整数组的大小。
您可以轻松创建一个没有空字符串和空引用的新数组,如下所示:
No, it's not possible without creating a new array. You can't resize an array.
You can easily create a new array without empty strings and null references like this:
如果您不想创建新数组,那么不,这是不可能的。您无法从简单数组中添加或删除项目(如
string[]
)。完成您想要实现的目标的最直接方法(如果删除第二个要求)将是:
srcArray
)设置为新数组If you don't want to create a new array, then no, it's not possible. You cannot add or remove an item from a simple array (as in,
string[]
).The most straightforward way to accomplish what you want to achieve (if you remove your second requirement) would be:
srcArray
) to your new array正如 Dan 所说,您无法在
Array
中添加或删除值。但是,您可以使用 LINQ 删除这些值并生成第二个数组。As Dan said, you can't add or remove values from an
Array
. You can, however, use LINQ to remove the values and produce a second array.可能不是性能最好的解决方案,但是......
它将创建一个新数组,但我想不出一个不会的解决方案。
Probably not the most performant solution but...
It will create a new array, but I cannot think of a solution that won't.
在决定如何继续之前,您确实需要考虑谁拥有对您正在操作的数组的引用。
如果该数组没有被任何其他引用代码(作为类的成员,作为 lambda 中捕获的变量,或在某个集合中)那么您不必担心创建新数组。在这种情况下,我会使用类似于 @Codesleuth 或 @Guffa建议。
但是,如果可能存在其他代码来保存对同一数组的引用,那么您就不走运了,除非您可以安全地识别和更新在其他位置保存的引用。这是一件很难做到的事情 - 并且您应该非常小心,假设您始终可以更新保存引用的所有其他位置。
Before deciding how to proceed, you really need to think about who holds a reference to the array you are operating on.
If the array is not referenced by any other code (as a member of a class, as a captured variable in a lambda, or in some collection somewhere) then you shouldn't worry about creating a new array. In that case I would use something like what @Codesleuth or @Guffa suggest.
However, if other code may exist that holds a reference to this same array - then you are out of luck, unless you can safely identify and update the references held in those other places. This is a hard thing to do - and you should be very careful assuming that you can always update all other places where a reference is held.
我是这里唯一一个会扫描数组并将成员移回 NULL 的人吗,从而制作一个连续的非空列表。
这不会创建新的数组,并且实现起来很简单,并且重要的是要知道您可以在数组中移动条目。
不幸的是,我正在工作,所以无法提供完整的代码,但是您可以通过在数组中搜索 NULL,然后将数组中的剩余项目向上移动一项来实现它。继续这样做直到最后。我建议在搜索完成后清除剩余的全部。
Am I the only one here that would scan the array and move the members back over the NULLs, therefore making a continuous list of non-nulls.
This doesn't create a new array and it's simple to implement and it's immportant to know you can move the entries around the array.
Unfortunately I'm at work so can not supply full code, however you would implement it by searching the array for NULLs then moving the remaining items in the array up one. Keep doing this until the end. I would suggest clearing the remaining entires once the search is completed.