zoukankan      html  css  js  c++  java
  • DotNet中的集合对象(1): ArrayList

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

    using System;
    using System.Collections;
    class TestArrayList
    {
        
    static void Main()
        
    {
            ArrayList theArrayList 
    = 
                
    new ArrayList();
            theArrayList.Add(
    "1");
            theArrayList.Add(
    "2");
            
    foreach(string s in theArrayList)
            
    {
                Console.WriteLine(s);
            }

        }

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

            theArrayList.Clear();
            Console.WriteLine(theArrayList.Count); 
            Console.Read();
        }

    }
        ArrayList中取出的对象都是object类型,使用前要将其转换成合适的类型.
    ArrayList theArrayList = new ArrayList();
    theArrayList.Add(
    "1");
    theArrayList.Add(
    "2");
    string s = (string)theArrayList[0];
    string s1 = (string)theArrayList[1];

         .Contains()    如果ArrayList中包含参数提供的对象,则返回true,否则返回false
        
    using System;
    using System.Collections;
    class TestArrayList
    {
        
    static void Main()
        
    {
            ArrayList theArrayList 
    = 
                
    new ArrayList();
            theArrayList.Add(
    "1");
            theArrayList.Add(
    "2");
            
    foreach(string s in theArrayList)
            
    {
                Console.WriteLine(s);
            }

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

            
    else
            
    {
                Console.WriteLine(
    "NO");
            }

            Console.Read();
        }

    }

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

            Console.Read();
        }

    }

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

    }
       Insert () 将元素插入到ArrayList指定的位置
     
    如果您喜欢本文,更多文章,欢迎订阅我的微信订阅号:open_dotNET
  • 相关阅读:
    我不是不懂
    Spring环境搭建,IoC容器初体验~
    备战招聘——信息获取与简历制作
    2013应届毕业生“华为”校招应聘总结
    Java知识积累——Sax解析xml文档
    Java知识积累——同时输出到控制台和指定文件,或直接输出到指定文件
    Java知识积累——静态代码块,非静态代码块,构造器的执行顺序和次数
    修改数据库表结构
    IE6/IE7/IE8/Firefox/Chrome/Safari的CSS hack兼容一览表
    项目代码风格要求
  • 原文地址:https://www.cnblogs.com/HuangLiang/p/216232.html
Copyright © 2011-2022 走看看