DotNet中的集合对象(1): ArrayList

 

让我们来看看DotNet中System.Collections名字空间包含的可变数组对象.
1)ArrayList(数组列表)
      本质上ArrayList对象就是一个可变长的数组,可以根据需要添加元素.使用ArrayList的方法可以向数组列表中添加元素,或取出,修改某个元素.
      .Add()方法
     

None.gif using  System;
None.gif
using  System.Collections;
None.gif
class  TestArrayList
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ArrayList theArrayList 
= 
InBlock.gif            
new ArrayList();
InBlock.gif        theArrayList.Add(
"1");
InBlock.gif        theArrayList.Add(
"2");
InBlock.gif        
foreach(string s in theArrayList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(s);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
    ArrayList有一个Count属性,可以确定它所包含的实际元素数目.
    .Clear                                            删除ArrayList中的内容
None.gif using  System;
None.gif
using  System.Collections;
None.gif
class  TestArrayList
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ArrayList theArrayList 
= 
InBlock.gif            
new ArrayList();
InBlock.gif        theArrayList.Add(
"1");
InBlock.gif        theArrayList.Add(
"2");
InBlock.gif        
foreach(string s in theArrayList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(s);
ExpandedSubBlockEnd.gif        }

InBlock.gif        theArrayList.Clear();
InBlock.gif        Console.WriteLine(theArrayList.Count); 
InBlock.gif        Console.Read();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
    ArrayList中取出的对象都是object类型,使用前要将其转换成合适的类型.
None.gif ArrayList theArrayList  =   new  ArrayList();
None.giftheArrayList.Add(
" 1 " );
None.giftheArrayList.Add(
" 2 " );
None.gif
string  s  =  ( string )theArrayList[ 0 ];
None.gif
string  s1  =  ( string )theArrayList[ 1 ];
None.gif
None.gif
     .Contains()    如果ArrayList中包含参数提供的对象,则返回true,否则返回false
    
None.gif using  System;
None.gif
using  System.Collections;
None.gif
class  TestArrayList
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ArrayList theArrayList 
= 
InBlock.gif            
new ArrayList();
InBlock.gif        theArrayList.Add(
"1");
InBlock.gif        theArrayList.Add(
"2");
InBlock.gif        
foreach(string s in theArrayList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(s);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if (theArrayList.Contains("1"))  //判断字符中"1"是否存在于ArrayList中
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            Console.WriteLine(
"Yes");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"NO");
ExpandedSubBlockEnd.gif        }

InBlock.gif        Console.Read();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

    CopyTo()    将ArrayList 全部内容拷贝到一个一维数组中
None.gif using  System;
None.gif
using  System.Collections;
None.gif
class  TestArrayList
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ArrayList theArrayList 
= 
InBlock.gif            
new ArrayList();
InBlock.gif        theArrayList.Add(
"1");
InBlock.gif        theArrayList.Add(
"2");
InBlock.gif        
string [] a = new string[2]; 
InBlock.gif        theArrayList.CopyTo(a);
InBlock.gif        
for (int i = 0; i < a.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(a[i]);
ExpandedSubBlockEnd.gif        }

InBlock.gif        Console.Read();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

     IndexOf()
None.gif using  System;
None.gif
using  System.Collections;
None.gif
class  TestArrayList
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ArrayList theArrayList 
= 
InBlock.gif            
new ArrayList();
InBlock.gif        
InBlock.gif        
string str1 = "1";
InBlock.gif        
string str2 = "2";
InBlock.gif        
InBlock.gif        
int num1 = 1;
InBlock.gif        
int num2 = 2;
InBlock.gif        
InBlock.gif        theArrayList.Add(str1);
InBlock.gif        theArrayList.Add(str2);
InBlock.gif        theArrayList.Add(num1);
InBlock.gif        theArrayList.Add(num2);
InBlock.gif        
InBlock.gif        Console.WriteLine(theArrayList.IndexOf(num1)); 
//返回改对象在ArrayList中的索引值
InBlock.gif
        Console.WriteLine(theArrayList.IndexOf(str1));
InBlock.gif        Console.Read();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
   Insert () 将元素插入到ArrayList指定的位置
 

转载于:https://www.cnblogs.com/HuangLiang/archive/2005/08/16/216232.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值