一位数组&二维数组&去重
1.二维数组的定义:
int[][] counts=new int[10][];
但是后面赋值的时候会报错,解决方法:counts[i]=new int[10];
int[,] counts=new int[2,3];
int[,] counts=new int[2,3]{{1,2,3},{3,2,1}}
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());