2011-10-27 199 views
0

我试图从arraylist中删除项目,但项目没有得到删除,我没有收到任何错误删除不工作。无法使用删除项目。删除

protected void ibtnMoveUp_Click(object sender, ImageClickEventArgs e) 
    { 
     ArrayList ImgArry = new ArrayList(); 
     ImgArry.Add(SelImgId); 
     ImgArry.Add(SelImgpath);//image name 
     ImgArry.Add(SelImgName);//image path 
     List<int> t1 = new List<int>(); 
     if (Imgarry1 != null) 
      t1 = Imgarry1;//Imaarry1 is the type List<int> 
     t1.Add(Convert.ToInt32(ImgArry[0])); 
     Imgarry1 = t1; 
     List<ArrayList> t = new List<ArrayList>(); 
     if (newpath.Count > 0)// newpath is the type List<ArrayList> nd creating the viewstate 
      t = newpath; 
     t.Remove(ImgArry);//Item is not getting remove 
     newpath = t; 
     for (int i = 0; i < newpath.Count; i++) 
     { 
      ArrayList alst = newpath[i]; 
      newtb.Rows.Add(Convert.ToInt32(alst[0]), alst[1].ToString(), alst[2].ToString(), i); 

     } 
     dlstSelectedImages.DataSource = newtb; 
     DataBind(); 
} 
+0

这是记者从昨日的答案:http://stackoverflow.com/questions/7907475/simplifying-locating-an-element-in-a-list-perhaps-using-linq/7907500 #7907500 –

回答

0

亚当Houldsworth是说写,但我有一些不同的方式完成第二我的代码是波纹管

我已删除t.Remove(ImgArry);该行并添加

   List<ArrayList> temp = new List<ArrayList>(t.Count); 
       for (int i = 0; i < t.Count; i++) 
       { 
        ArrayList al = t[i]; 
        if (Convert.ToInt32(al[0]) != Convert.ToInt32(ImgArry[0])) 
         temp.Add(al); 
       } 
       t = temp; 
1

删除正在工作,但您传递的项目未通过与列表中的任何项目进行相等性测试。

通过提供对象进行删除将尝试对该项目的所有项目进行等同性检查(通常通过.Equals()),直到找到一个项目,然后将其删除。如果没有找到,它不会引起异常。

+0

你会PLZ告诉我我怎样才能删除该项目 – Rocky

+0

@摇滚你的示例代码没有证明问题。 'newpath'有条件地分配给't',它可能有或没有任何未知类型的项目。你的问题目前不能直接回答。我只能回答为什么'删除'是“失败”。 –

+0

当我手动检查项目是不是从列表中删除,我的平等是通过 – Rocky

0

ImgArry是一个局部变量,引用类型。由于Equals()的参考类型的默认行为的确是ReferenceEquals(),所以无法实现它,因为刚创建的实例无论如何都不能放入容器中。

您必须搜索您想要先删除的项目。例如。 :t.Find(a => a[0] == SelImgId)

然后你可以t.Remove()以前退回的项目。