1.前台代码:
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>数组删除重复值</title>
</head>
<body>
<formid="form1"runat="server">
<div>
数组删除前:
<asp:LabelID="lblResult1"runat="server"></asp:Label>
<br/>
数组删除后:
<asp:LabelID="lblResult2"runat="server"></asp:Label>
</div>
</form>
</body>
</html>
2.后台代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Collections;//引用
publicpartialclassNetObjects_数组_删除重复值:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
stringstrNum="168,145,150,148,333,888,666,168,144";
//输出原数组
lblResult1.Text=strNum;
string[]arrNum=strNum.Split(',');
ArrayListal=newArrayList();
for(inti=0;i<arrNum.Length;i++)
{
//判断数组值是否已经存在
if(al.Contains(arrNum[i])==false)
{
al.Add(arrNum[i]);
}
}
//把ArrayList转换数组
arrNum=newstring[al.Count];
arrNum=(string[])al.ToArray(typeof(string));
//输出删除后数组
stringresult="";
for(intj=0;j<arrNum.Length;j++)
{
if(j!=0)
{
result+=",";
}
result+=arrNum[j];
}
lblResult2.Text=result;
}
}
|