html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
title>數(shù)組刪除重復(fù)值/title>
/head>
body>
form id="form1" runat="server">
div>
數(shù)組刪除前:
asp:Label ID="lblResult1" runat="server">/asp:Label>
br />
數(shù)組刪除后:
asp:Label ID="lblResult2" runat="server">/asp:Label>
/div>
/form>
/body>
/html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections; //引用
public partial class NetObjects_數(shù)組_刪除重復(fù)值 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strNum = "168,145,150,148,333,888,666,168,144";
//輸出原數(shù)組
lblResult1.Text = strNum;
string[] arrNum = strNum.Split(',');
ArrayList al = new ArrayList();
for (int i = 0; i arrNum.Length; i++)
{
//判斷數(shù)組值是否已經(jīng)存在
if (al.Contains(arrNum[i]) == false)
{
al.Add(arrNum[i]);
}
}
//把ArrayList轉(zhuǎn)換數(shù)組
arrNum = new string[al.Count];
arrNum = (string[])al.ToArray(typeof(string));
//輸出刪除后數(shù)組
string result = "";
for (int j = 0; j arrNum.Length; j++)
{
if (j != 0)
{
result += ",";
}
result += arrNum[j];
}
lblResult2.Text = result;
}
}