从2D(二维)数组中删除行 [英] Remove row from 2D(Two Dimensional) array

查看:96
本文介绍了从2D(二维)数组中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从2 D数组的第一列中找到最大值,然后将此行转换为1d数组,最后从2d数组中删除行,我需要多次重复此过程,所以每次我都要删除行发现最大值,例如这是我的2d数组{{3,5,7},{4,9,8},5,9,1}}所以我想制作1d数组和2d数组将是{{3 ,5,7},{5,9,1}}任何帮助请

解决方案

在.NET中,你不能改变数组的大小,它的等级和所以 - 你只能修改数组的任何元素。



如果你想添加/删除任何内容,请使用 collections 代替例如,最简单的一个, System.Collections.Generic.List<> 。请注意,当您填充它时, List 的实例可以转换为数组。



不要混淆使用方法 System.Array.Resize - 尽管这个名字(另一个令人困惑的微软名称!),它不会调整任何大小;相反,它创建了一个全新的阵列,在几乎所有情况下都太昂贵而不能认真对待,更好地使用集合。创建新数组实例的事实在MSDN帮助页面的备注部分中提到, Array.Resize(T)方法(T [],Int32)(系统) [ ^ ]。



数组仅限当你创建一个数组实例时,事先知道每个维度的等级和长度,这很好。



-SA

I have to Find max value from the first column of 2 D array and then convert this row to 1d array and finally delete row from 2d array and I need to repeat this process many times so I have to delete row each time I found the max for example this is my 2d array { {3,5,7},{4,9,8},5,9,1}} so I suppose to make as 1d array and 2d array will be {{3,5,7},{5,9,1}} any help please

解决方案

In .NET, you cannot change the size of an array, its rank and so on — you can only modify any element of the array.

If you want to add/remove anything, use collections instead, for example, the simplest one, System.Collections.Generic.List<>. Note that the instance of List can be converted to array when you populate it.

Don't be confused with the method System.Array.Resize — despite this name (yet another confusing Microsoft name!), it does not resize anything; instead, it creates a brand new array, which is too expensive to take seriously, better use collection, in nearly all cases. The fact that the new array instance is created is mentioned in the "Remark" section of the MSDN help page, Array.Resize(T) Method (T[], Int32) (System)[^].

Arrays are only good when you know the rank and the length in each dimension in advance, when you create an instance of an array.

—SA


这篇关于从2D(二维)数组中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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