日期:2014-05-19  浏览次数:20751 次

数组的replace如何实现?
int   []   a   =   {1,2,0,4,0,5,6,7};
现在我要把数组中的0的元素都去掉,怎么做?

------解决方案--------------------
try

int[] a = { 1, 2, 0, 4, 0, 5, 6, 7 };
List <int> list = new List <int> ();
foreach (int i in a)
{
if (i != 0)
list.Add(i);
}
int[] b = list.ToArray();
------解决方案--------------------
int[] a = { 1, 2, 0, 4, 0, 5, 6, 7 };
List <int> list = new List <int> ();
for (int i = 0; i < a.Length; i++)
{
if (a[i] != 0)
list.Add(i);
}
int[] b=new int[list.Count];
Array.Copy(list.ToArray(), b,b.Length);