无法使用删除项目。删除
问题描述:
我试图从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();
}
答
亚当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;
答
ImgArry
是一个局部变量,引用类型。由于Equals()
的参考类型的默认行为的确是ReferenceEquals()
,所以无法实现它,因为刚创建的实例无论如何都不能放入容器中。
您必须搜索您想要先删除的项目。例如。 :t.Find(a => a[0] == SelImgId)
然后你可以t.Remove()
以前退回的项目。
这是记者从昨日的答案:http://*.com/questions/7907475/simplifying-locating-an-element-in-a-list-perhaps-using-linq/7907500 #7907500 –