我想把一个ArrayList里面的每一项先打印出来,再删除该项,用
for(int i=0; i<MyArrayList.Count; ++i)
{
MessageBox.Show(MyArrayList[i].ToString());
MyArrayList.RemoveAt(i);
}
这样不行,大家有什么办法吗?
for(int i=0; i<MyArrayList.Count; ++i)
{
MessageBox.Show(MyArrayList[i].ToString());
MyArrayList.RemoveAt(i);
}
这样不行,大家有什么办法吗?
{
MessageBox.Show(MyArrayList[i].ToString());
}
MyArrayList.Clear();
{
MessageBox.Show(obj.ToString());
MyArrayList.Remove(obj);
}
while (MyArrayList.Count > 0)
{
MessageBox.Show(MyArrayList[0].ToString());
MyArrayList.RemoveAt(0);
}
while (list.Count != 0)
{
Console.WriteLine(list[0].ToString());
list.RemoveAt(0);
}
{
MessageBox.Show(obj.ToString());
MyArrayList.Remove(obj);
}
这样业不行
{
MessageBox.Show(MyArrayList[i].ToString());
MyArrayList.RemoveAt(i);
}
Remove后Count会变
用While循环
{
MessageBox.Show(ar[0].ToString());
ar.RemoveAt(0);
i = i - 1;
}
?
to flyaqiaoforeach (object obj in MyArrayList)
{
MessageBox.Show(obj.ToString());
MyArrayList.Remove(obj);
}
这样业不行============================
这个答案是纯粹误导楼主foreach循环是只读循环,目的是为了达到更快的访问速度,所以在foreach内不能修改直接对象数组.
for(int i=0;i<arrlen;i++)
{
MessageBox.Show(ar[0].toString());
ar.RemoveAt(0);
}
你解释foreach 循环,解释的很好,给我重新一个认识.开始我也想到了foreach,但是确实不行.
{
MessageBox.Show(arraylist[0].toString());
arraylist.RemoveAt(0);
}
{
MessageBox.Show(arraylist[0].toString());
arraylist.RemoveAt(0);
}
{
MessageBox.Show(MyArrayList[i].ToString());
MyArrayList.RemoveAt(i);
}
for(int i=0; i<clone.Count; ++i)
{
MessageBox.Show(clone[i].ToString());
MyArrayList.RemoveAt(i);
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 最新版本:20070212http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
for(int i=0; i<clone.Count; ++i)
{
MessageBox.Show(clone[i].ToString());
MyArrayList.RemoveAt(i);
}
----------
这个行吗?到后来MyArrayList.RemoveAt(i);是不是会出错啊,因为这个时候MyArrayList里面的元素的个数很可能已经小于i了
{
MessageBox.Show(MyArrayList[i].ToString());
MyArrayList.RemoveAt(i);
}
{
MessageBox.Show(MyArrayList[i].ToString());
MyArrayList.RemoveAt(i);
}
for(int i=0; i<clone.Count; ++i)
{
MessageBox.Show(clone[i].ToString());
MyArrayList.RemoveAt(0);
}
for(int i=MyArrayList.Count-1; i>=0; i--)
{
MessageBox.Show(MyArrayList[i].ToString());
MyArrayList.RemoveAt(i);
}
{
object o=ar[i];
if(del)//o满足一定条件
{
MessageBox.Show(o.ToString());
ar.Remove(o);
i = i--;
}
}
//我想这样才是这个问题的真正意义
----------
是喝多了,呵呵
{
object o=ar[i];
if(del)//o满足一定条件
{
MessageBox.Show(o.ToString());
ar.Remove(o);
i = i--;
}
}
-----------
那句判断是在很多情况下用到的,你这中解法类似C++中vector的满足条件的删除
{
MessageBox.Show(MyArrayList[i].ToString());
}
MyArrayList.Clear();
ok
{
MessageBox.Show( arraylist[0] );
arraylist.RemoveAt( 0 );
}
{
MessageBox.Show( arraylist[0] );
arraylist.RemoveAt( 0 );
}
{
MessageBox.Show(MyArrayList[i].ToString());
}
MyArrayList.Clear();
ok
---------------
这个不满足需要,有可能我在for循环里面判断条件就break了,而break之前的删除了MyArrayList中的一些,没有完全删除完毕,剩下的要保留起来,不能clear
{
MessageBox.Show(MyArrayList[0].ToString());
MyArrayList.RemoveAt(0);
}