社区
C#
帖子详情
如何清空一个ArrayList类型的数组?
tob
2003-09-23 09:15:17
ArrayList Arr1;
怎样清空这个数组,好像用Arr1.clear()不行。
用Array.Clear(Arr1,0,Arr1.Capacity)也不行,怎样清空ArrayList的数组呢?
...全文
4410
10
打赏
收藏
如何清空一个ArrayList类型的数组?
ArrayList Arr1; 怎样清空这个数组,好像用Arr1.clear()不行。 用Array.Clear(Arr1,0,Arr1.Capacity)也不行,怎样清空ArrayList的数组呢?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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();
生成实例时不是空了吗?
c#
数组
与集合(
ArrayList
)游戏开发高级使用举例
在实际应用中,我们可以通过创建
一个
模拟的游戏场景来演示如何使用
数组
和
ArrayList
。例如,可以创建
一个
大提琴音乐节的模拟游戏,其中
数组
用来存储参赛选手的初始排名,
ArrayList
用来存储比赛过程中动态变化的排名。...
数组
和集合对象(三)
ArrayList
练习
8.
清空
ArrayList
: ```csharp list.Clear(); // 删除所有元素 ``` 通过这个练习,我们可以掌握
ArrayList
的基本用法,同时了解如何处理不同
类型
的对象。然而,
ArrayList
虽然方便,但效率较低,因为它不是泛型集合。...
你必须知道的C#
ArrayList
-
ArrayList
内部维护了
一个
Object
类型
的
数组
,当添加或删除元素时,它会自动调整
数组
大小以适应变化。 2. **
ArrayList
的主要方法** - `Add(object item)`: 向
ArrayList
末尾添加
一个
元素。 - `Insert(int index, ...
C#的动态
数组
介绍可用
`
ArrayList
` 是
一个
对象
数组
,可以存储任何
类型
的对象,并且提供了许多用于添加、删除、插入和检索元素的方法。下面我们将详细介绍 `
ArrayList
` 的一些关键特性和用法。 #### 创建
ArrayList
```csharp
ArrayList
...
集合类
ArrayList
和HashTable实例操作练习
Add()方法用于将单个元素追加至
ArrayList
的末端,而AddRange()方法则用于批量添加多个元素,比如
一个
数组
或另
一个
ArrayList
对象。 需要留意的是,尽管
ArrayList
的元素
类型
被声明为object,这表示它可以容纳任何
类型
...
C#
111,120
社区成员
642,537
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章