C# 数组列表问题

C# 数组列表问题,c#,arraylist,C#,Arraylist,如何检查数组列表中的null并将其删除?它不断给我一个错误“ArgumentOutOfRange已处理”,或者如果我将SMTPException更改为just Exception,它会显示“索引超出范围”。查看调试器并特别查看邮件。Count它为我提供Count=4。我最初给它发了3封电子邮件要检查。最后一个数组是“”。我相信这是问题的根源 private void button1_Click(object sender, EventArgs e) { try

如何检查数组列表中的null并将其删除?它不断给我一个错误“ArgumentOutOfRange已处理”,或者如果我将SMTPException更改为just Exception,它会显示“索引超出范围”。查看调试器并特别查看邮件。Count它为我提供Count=4。我最初给它发了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";
                }
            }
        }
private void按钮1\u单击(对象发送者,事件参数e)
{
尝试
{
如果(textBox1.Text==“”)
{
textBox3.Text+=“[-]列表框为空!!!!\r\n”;
返回;
}
//textBox1.Text.Split(新字符串[]{“\n”},StringSplitOptions.RemoveEmptyEntries);
//获取textbox1 amd中提供的电子邮件,然后使用“\n”分隔阵列
ArrayList邮件=新的ArrayList(textBox1.Text.Split('\n'));
//注:供思考
//IEnumerable myResults=mails.Split('\n')。其中(s=>!string.IsNullOrEmpty);
for(int i=0;i

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

我认为空条目是由文本框中最后一项后的回车符引起的。

尝试如下填充数组:

ArrayList mails = new ArrayList(textBox1.Text.Trim().Split('\n'));
foreach(object o in ArrayList)
{

}

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

通过检查条件从列表中删除项可能很棘手。如果您没有使用漂亮的LINQ语句(因为您使用的是ArrayList,我将假设LINQ对您不可用),则需要使用反向迭代

如果您对如下列表进行迭代:

ArrayList mails = new ArrayList(textBox1.Text.Trim().Split('\n'));
foreach(object o in ArrayList)
{

}

for(int i=0;i
删除项目时会遇到问题,因为项目将用尽(因为列表现在比定义for/foreach时小)

您需要使用反向迭代或存储项目列表,以便稍后删除

示例1:反向迭代

for(int i = myArray.Count - 1; i >= 0; i--)
{ 
   object o = myArray[i];
   if (<some condition here>)
       myArray.Remove(i);
}
for(int i=myArray.Count-1;i>=0;i--)
{ 
对象o=myArray[i];
如果()
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);
ArrayList toRemove=new ArrayList();
foreach(myArray中的对象o)
{
if()
删除。加入(o);
}
foreach(toRemove中的对象o)
myarray.Remove(o);
示例3:LINQ方法

var cleanEntities = myArray.Where(x => <some condition on x, which is your item>);
var cleanEntities=myArray.Where(x=>);

< /代码> 在构建另一个列表时,通常更简单地重复一个列表。很难出错。

我会礼貌地建议扔掉<代码> ARARYLIST/CODE >,并将其替换为<代码>列表 >,如果只是使代码不太过时。AlrayList在2004年末已经过时了。请考虑替换。使用新的通用列表等价物
list
(在您的情况下)。这将消除您对所有
.ToString()的需要
s到处都是。@Steven,Jesse。很好的建议我以前使用过这三种方法,每种方法都取得了同样的成功。说得很好,Are。我对LINQ还是很陌生,但我喜欢你很快就能做到这一点。我以后会尝试一下。它不能是回车,因为它是一个字符串,用于文本框,与方法。另一方面,数组没有被馈送任何回车符,只返回换行符。这正是我要找的!继续!不再崩溃=)