代码之家  ›  专栏  ›  技术社区  ›  Nightforce2

数组列表问题

  •  0
  • Nightforce2  · 技术社区  · 15 年前

    如何在数组列表中检查null并删除它们?它一直给我一个错误“ArgumentOutOfRange was handled”,或者如果我将SMTPException改为just Exception,它会说“Index out of range”。查看调试器并特别查看邮件。我最初给它发了3封邮件要检查。最后一个数组是“”。我认为这是问题的根源。

    private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text == "")
                {
                    textBox3.Text += "[-] Listbox is Empty!!!!\r\n";
                    return;
                }
                // textBox1.Text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); 
                // Grabs Emails supplied in textbox1 amd then seperates array using '\n'
                ArrayList mails = new ArrayList(textBox1.Text.Split('\n')); 
    
                // Note: For thought 
                // IEnumerable<string> myResults = mails.Split('\n').Where<string>(s => !string.IsNullOrEmpty(s));
                for (int i = 0; i < mails.Count; i++)
                {
                    // Seperates user & pass to be passed for authentification.
                    ArrayList mailInfo = new ArrayList(mails[i].ToString().Split(':'));
                    textBox3.Text += "[+] Checking email format for" + "\r\n" + "[/] " +  mails[i] + "\r\n";
                    // Attach domain name if not attached.
                    if (!mailInfo[0].ToString().EndsWith("@gmail.com")) mailInfo[0] = mailInfo[0] + "@gmail.com"; 
    
                    // Debug:  Check point
                    // 
                    // Error message:
                    // Index was out of range. Must be non-negative and less than the size of the collection. Parameter name index. 
                    // mails.Count = 4 when feeding only 3 emails.
                    //
                    // Function:  Check mail & Password
                    // error: index out of range
                    // MessageBox.Show(mailInfo[0].ToString() + "\n\n" + mailInfo[1].ToString());
                    if (mails[i] == "") throw new ArgumentOutOfRangeException("No Mail", "No more mail to check.");
                    if (checkAccount(mailInfo[0].ToString(), mailInfo[1].ToString()))
                    {
                        textBox3.Text += "[+] Connection Successful! Checking mail.: mailInfo[0].ToString()\r\n";
                    }
                }
            }
    

    我做错什么了???空值是我的问题吗?如果是的话,我该如何删除它或者我遗漏了什么?

    4 回复  |  直到 13 年前
        1
  •  0
  •   spinon    15 年前

    尝试按以下方式填充数组:

    ArrayList mails = new ArrayList(textBox1.Text.Trim().Split('\n'));
    

    这将删除所有尾随空格,并将最后一项从数组中删除。

        2
  •  2
  •   Aren    15 年前

    foreach(object o in ArrayList)
    {
    
    }
    

    或者

    for(int i = 0; i < myArray.Count; i++)
    {
    }
    

    删除项目时会遇到问题,因为项目将用完(因为列表现在比定义for/foreach时小)。

    for(int i = myArray.Count - 1; i >= 0; i--)
    { 
       object o = myArray[i];
       if (<some condition here>)
           myArray.Remove(i);
    }
    

    示例2:删除列表*

    ArrayList toRemove = new ArrayList();
    foreach(object o in myArray)
    {
       if(<some condition here>)
         toRemove.Add(o);
    }
    
    foreach(object o in toRemove)
       myarray.Remove(o);
    

    示例3:LINQ方法

    var cleanEntities = myArray.Where(x => <some condition on x, which is your item>);
    
        3
  •  0
  •   S P    15 年前

        4
  •  0
  •   Steven Sudit    15 年前

    在建立另一个列表的同时迭代一个列表通常比较简单。那样很难出错。