如何清空一个ArrayList类型的数组?

tob 2003-09-23 09:15:17
ArrayList Arr1;
怎样清空这个数组,好像用Arr1.clear()不行。
用Array.Clear(Arr1,0,Arr1.Capacity)也不行,怎样清空ArrayList的数组呢?
...全文
4410 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
大户翁 2003-09-23
  • 打赏
  • 举报
回复

如 ArrayList.clear() 不行; 则用 ArrayList=null;
Soking 2003-09-23
  • 打赏
  • 举报
回复
ArrayList.clear
hpfeng 2003-09-23
  • 打赏
  • 举报
回复
Clear()行的,我也用过
acewang 2003-09-23
  • 打赏
  • 举报
回复
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumped" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );

// Displays the ArrayList.
Console.WriteLine( "The ArrayList initially contains the following:" );
PrintValues( myAL );

// Removes the element containing "lazy".
myAL.Remove( "lazy" );

// Displays the current state of the ArrayList.
Console.WriteLine( "After removing \"lazy\":" );
PrintValues( myAL );

// Removes the element at index 5.
myAL.RemoveAt( 5 );
kfy0002 2003-09-23
  • 打赏
  • 举报
回复
用clear()行,我试过!

private void button1_Click(object sender, System.EventArgs e)
{
System.Collections.ArrayList arr = new System.Collections.ArrayList();
arr.Add("aa");
arr.Add("bb");
MessageBox.Show(arr.Count.ToString());
arr.Clear();
MessageBox.Show(arr.Count.ToString());

}
acewang 2003-09-23
  • 打赏
  • 举报
回复
using System;
using System.Collections;
public class SamplesArrayList {

public static void Main() {

// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumped" );

// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "Initially," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );

// Trim the ArrayList.
myAL.TrimToSize();

// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "After TrimToSize," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );

// Clear the ArrayList.
myAL.Clear();

// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "After Clear," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );

// Trim the ArrayList again.
myAL.TrimToSize();

// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "After the second TrimToSize," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
}

public static void PrintValues( IEnumerable myList ) {
System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "\t{0}", myEnumerator.Current );
Console.WriteLine();
}
}
tob 2003-09-23
  • 打赏
  • 举报
回复
Arr1.clear();
根本没有删到数组的内容。
acewang 2003-09-23
  • 打赏
  • 举报
回复
ArrayList.clear
ArrayList.remove(Index)
ArrayList.removtAt()
timmy3310 2003-09-23
  • 打赏
  • 举报
回复
Arrl.Clear();

是用这个方法,你说不行是怎么个不行法?
myall2002 2003-09-23
  • 打赏
  • 举报
回复
ArrayList Arrl = new ArrayList();
生成实例时不是空了吗?

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧