在ASP.NET編程中,要想刪除數組的重復值可以使用多種方法代碼實現相同的效果。今天,在某個博客中看到某功能代碼中的一小段代碼很不錯,它就是用來移動數組中相同值的方法,分享給大家
根據這段代碼,自己編寫了一個小程序作為代碼資料參考,方便以后可以直接拿來用,不需要網上找。如果你覺得還不錯的話,就把它收藏起來吧!
1.前臺代碼:
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>數組刪除重復值</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- 數組刪除前:
- <asp:Label ID="lblResult1" runat="server"></asp:Label>
- <br />
- 數組刪除后:
- <asp:Label ID="lblResult2" runat="server"></asp:Label>
- </div>
- </form>
- </body>
- </html>
2.后臺代碼:
- 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_數組_刪除重復值 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string strNum = "168,145,150,148,333,888,666,168,144";
- //輸出原數組
- lblResult1.Text = strNum;
- string[] arrNum = strNum.Split(',');
- ArrayList al = new ArrayList();
- for (int i = 0; i < arrNum.Length; i++)
- {
- //判斷數組值是否已經存在
- if (al.Contains(arrNum[i]) == false)
- {
- al.Add(arrNum[i]);
- }
- }
- //把ArrayList轉換數組
- arrNum = new string[al.Count];
- arrNum = (string[])al.ToArray(typeof(string));
- //輸出刪除后數組
- string result = "";
- for (int j = 0; j < arrNum.Length; j++)
- {
- if (j != 0)
- {
- result += ",";
- }
- result += arrNum[j];
- }
- lblResult2.Text = result;
- }
- }
3.最終輸出效果:
以上就是關于ASP.NET數組刪除重復值的實現方法,希望對大家的學習有所幫助。
新聞熱點
疑難解答
圖片精選