首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#除去重复项

2013-01-01 
c#去除重复项using Systemusing System.Collections.Genericusing System.ComponentModelusing System.

c#去除重复项
c#除去重复项

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WindowsFormsApplication130
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Dictionary<string, string> dic = new Dictionary<string, string>();
        string[] str;
        ArrayList array = new ArrayList();
        
        private void Form1_Load(object sender, EventArgs e)
        {
            str = new string[] { "wo", "wo", "ni", "ni", "ta", "ta", "en", "en", "hehe", "shidi", "en" };
        }
        public ArrayList  a()
        {
            for (int i = 0; i < str.Length; i++)
            {
                try
                {
                    dic.Add(str[i], "w");
                    array.Add(str[i]);
                }
                catch
                { }
                finally
                { }
            }
            return array;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] s =new string [a ().Count ] ;
            for (int i = 0; i < s.Length; i++)
            {
                s[i] = array[i].ToString();


            }
                this.richTextBox1.Lines = s;
        }
    }
}



感觉还不错的说,分享....
[解决办法]
这不是有Linq的去掉相同项Distinct()么。。。
[解决办法]
.net 3.5以上直接用:
string[] strs = new string[] { "wo", "wo", "ni", "ni", "ta", "ta", "en", "en", "hehe", "shidi", "en" };
strs = strs.Distinct().ToArray();
[解决办法]
我看到楼主用.net是支持linq的:using System.Linq;
所以用我这个就行了,Linq的去重复无论开发效率还是运行效率都极其高效。

热点排行