Thursday, January 15, 2009

去除数组中重复的值,在组成新的数组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;//

namespace ArrayAndArrayList
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[9];
arr[0] = 1;
arr[1] = 9;
arr[2] = 7;
arr[3] = 7;
arr[4] = 8;
arr[5] = 9;
arr[6] = 3;
arr[7] = 9;
arr[8] = 10;

ArrayList al = new ArrayList();
for (int i = 0; i < arr.Length; i++)
{
bool b = false;
int temp = arr[i];
for (int j = i + 1; j < arr.Length; j++)
{
if (temp == arr[j])
{
b = true;
}
}
if (!b)
{
al.Add(temp);
}
}
//for (int i = 0; i < arr.Length; i++)
//{
// for (int j = i + 1; j < arr.Length; j++)
// {
// if (arr[i] != arr[j])
// {
// b = false;
// }
// else
// {
// b = true;
// i++;
// }
// }
// if (b == false)
// {
// al.Add(arr[i]);
// }
//}

Console.WriteLine(al.Count);
foreach (int i in al)
{
Console.WriteLine(i);
}
}
}
}

No comments: