zoukankan      html  css  js  c++  java
  • 数组和集合对象

    1 System.Array介绍

      Array类是一个抽象的基类,我们不能用如下方式创建一个Array类的实例

    Array myArray=new Array();

      但它提供了CreateInstance构建数组

    Array myArray=Array.CreateInstance(typeof(string),10);

    2.System.Array方法

      BinarySearch 使用二进制搜索方法搜索一维排序数组中的某人值

      Clear 将数组中的一组元素设为0或null

      CreateInstance 初始化Array类的新实例

      GetLength 指定给定维数的数组的元素总个数

      GetLowerBound 指定给定数组的下界

      GetUpperBound 指定给定数组的上界

      GetValue 返回给定数组中指定元素的值,该元素可通过指定位置索引来指明

      IndexOf 返回给定值在一维数组中第一次出现时的位置索引

      Sort 对数组元素进行排序

    3.System.Collections 介绍

      System.Collections 是一个命名空间,这个命名空间提供一组接口和类,使用户能够对一组数据元素执行集合操作

      类:

      Hashtable:存储键值对,这些键值对是根据键编码来组织的

      SortedList:组织对象并允许通过索引或键值来访问这些对象

      接口:

      ICollection 为所有集合定义大小、枚举器和同步方法

      IEnumerator 对整个集合的简单循环和列举,可以使用派生自IEnumerator 的IDictionaryEnumerator 来枚举字典中的元素

      IList 通过索引进行单独访问的对象的集合

      结构:

      DictionaryEntry 定义可以设置或检索的字典键值对

    4.Hashtable 类

      Hashtable 类将数据作为一组键值对来存储,这些键值对是根据键的编码来组织的

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    
    namespace BaseConsole
    {
        
        public partial class HashtableExample : Form
        {
            private Hashtable _hashtable = new Hashtable();
            public HashtableExample()
            {
                InitializeComponent();
            }
          
    
            private void btnAdd_Click(object sender, EventArgs e)
            {
                try
                {
                    //将元素添加到哈希表
                    _hashtable.Add(txtKey.Text,txtValue.Text);
                }
                catch(Exception ex)
                {
                    MessageBox.Show("产生系统错误。");//产生未知错误
                    Application.Exit();
                }
                lstAddedKey.Items.Add(txtKey.Text);
            }
    
            private void lstAddedKey_SelectedIndexChanged(object sender, EventArgs e)
            {
                string selectedItem = lstAddedKey.SelectedItem.ToString();
                string selectedValue = (string)_hashtable[selectedItem];//检索对象
                txtSelectedValue.Text = selectedValue;
            }
        }
    }

    结果:

    5.ArrayList 类

      Array类属于System命名空间,ArrayList类属于System.Collections命名空间

       属性:

       Capacity:指定数组列表可以包含的元素个数,默认容量为16

       方法:

       Contains:检查给定元素是否属于数组列表

       Insert:将给定元素插入数组列表中指定的索引位置

       Remove:从数组列表中移除第一次出现的给定对象

       RemoveAt:移除数组列表中特定索引位置的元素

       TrimToSize:定义数组列表中实际元素个数

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    /**/
    using System.Collections;
    
    namespace BaseConsole
    {
        public partial class frmArrayListExample : Form
        {
            private ArrayList _arrPerson = new ArrayList();
            public frmArrayListExample()
            {
                InitializeComponent();
            }
          
            private void btnAdd_Click(object sender, EventArgs e)
            {
                _arrPerson.Add(this.txtPersonName.Text);
                lstMessage.Items.Clear();
                lstMessage.Items.Add("数组的容量是:"+this._arrPerson.Capacity.ToString());
                lstMessage.Items.Add("数组的元素个数是:"+this._arrPerson.Count.ToString());
                lstMessage.Items.Add("---------------------------------------------------");
                foreach(string personName in _arrPerson)
                {
                    lstMessage.Items.Add("元素:"+personName);
                }
                lstMessage.Items.Add("---------------------------------------------------");
                _arrPerson.TrimToSize();
                lstMessage.Items.Add("整理后的数组容量是:"+_arrPerson.Capacity.ToString());
            }
    
            private void btnSearch_Click(object sender, EventArgs e)
            {
                if (_arrPerson.Contains(txtSearchString.Text))
                {
                    MessageBox.Show("存在这个元素。");
                }
                else
                {
                    MessageBox.Show("不存在这个元素。");
                }
            }
        }
    }

     结果:

     

  • 相关阅读:
    python 项目实例
    flash教程
    flask request
    systemd-unit
    kubernets HA集群手动部署
    zookeeper(1)-简单介绍
    apache与nginx原理
    技术文章整理
    CMS垃圾回收器
    Zookeeper
  • 原文地址:https://www.cnblogs.com/dong897812629/p/2871557.html
Copyright © 2011-2022 走看看