首页 > 编程语言 > 详细

一位数组&二维数组&去重

时间:2017-03-09 17:03:47      阅读:438      评论:0      收藏:0      [点我收藏+]

1.二维数组的定义:int[][] counts=new int[10][];
但是后面赋值的时候会报错,解决方法:counts[i]=new int[10];

int[][] array2 = { };
array2[0][0] = 1;//array2无值,所以不存在array[0][0],所以报错:索引超出了数组界限

2.一维数组的定义:int[] count=new int[10];
对数组进行去重:
(1)int[] b = new int[] { 2,2,4,2,5,3,5,5};
int[] a = arrt.Distinct().ToArray();//去除一维数组中重复的
(2)int[] a_new = DelRepeatData(b);
static int[] DelRepeatData(int[] a)
{
return a.GroupBy(p => p).Select(p => p.Key).ToArray();
}
(3)int[] arrt = { 5,2,6,54,3,5,2,5};
List<int> arrt1 = arrt.ToList();
for (int i = 0; i < arrt1.Count(); i++)
{
for (int j = i+1; j < arrt1.Count(); j++)
{
if (arrt1[i] == arrt1[j])
{
arrt1.RemoveAt(j);
j--;//如果该数据相等,去除掉了该下标元素,下一个元素就会相应的补上,但
由于该元素还没经判断,但其下标已经进行了判断,所以容易出错,如果判断元素相等再j--,就会再次判
断该下标的的元素
}
}
}

 

(1)string[] aa1 = {"a","123","aa","bgf","a","s","123" };
string[] a2 = aa1.Distinct().ToArray();
(2)List<string> attr = aa1.ToList();
for (int i = 0; i < attr.Count(); i++)
{
for (int j = i + 1; j < attr.Count(); j++)
{
if (attr[i] == attr[j])
{
attr.RemoveAt(j);
j--;
}
}
}

string转List:
List<string> result = new List<string>(aa.Split(new string[]{","},StringSplitOptions.RemoveEmptyEntries));
List转string
string bb = string.Join(",", result.ToArray());

一位数组&二维数组&去重

原文:http://www.cnblogs.com/Zbuxu/p/6526381.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!