<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        集合類Array List HashTable實例操作練習

        來源:懂視網 責編:小采 時間:2020-11-27 22:41:37
        文檔

        集合類Array List HashTable實例操作練習

        集合類Array List HashTable實例操作練習:集合常用操作添加、遍歷、移除 命名空間System.Collections ArrayList 可變長度數組,使用類似于數組 屬性 Capacity Count 方法 Add() AddRange() Remove() RemoveAt() Clear() Contains() ToArray() Hashtable 鍵值
        推薦度:
        導讀集合類Array List HashTable實例操作練習:集合常用操作添加、遍歷、移除 命名空間System.Collections ArrayList 可變長度數組,使用類似于數組 屬性 Capacity Count 方法 Add() AddRange() Remove() RemoveAt() Clear() Contains() ToArray() Hashtable 鍵值

        集合常用操作添加、遍歷、移除
        命名空間System.Collections

        ArrayList 可變長度數組,使用類似于數組
        屬性 Capacity Count
        方法
        Add() AddRange() Remove() RemoveAt() Clear()
        Contains() ToArray()
        Hashtable 鍵值對(KeyValuePair)的集合,類似于字典

        a、ArrayList對值類型的操作
        代碼如下:


        using System;
        using System.Collections;
        namespace _08_ArrayList {
        //ArayList對值類型的操作
        class Program {
        static void Main( string[] args) {
        //ArrayList與數組沒多大的區別 優點在于不像數組需規定長度 缺點是數據類型不限制 什么類型數據都可以放入 這樣會出現許多錯誤
        ArrayList arylist = new ArrayList();
        //ArrayList添加
        arylist.Add(1000);
        //arylist.Add("張三");//參數類型為object 所以可以添加多種類型的參數 取出時同樣需要類型轉換
        arylist.Add(3000);
        arylist.Add(4000); //發生裝箱操作 將值類型轉換引用類型
        arylist.Add(5000);
        int [] arr = { 1, 2, 3, 4 };
        arylist.AddRange(arr); //AddRange參數是實現了ICollections接口的對象 可以一次性添加數組、array、ArrayList等實現接口的對象
        //集合中元素個數 使用Count = 數組Length
        Console .WriteLine("集合內容長度" + arylist.Count);
        //Capacity為集合的容量 是可變的 一般*2增長
        Console .WriteLine(arylist.Capacity);
        //訪問集合第一個元素
        int firstlist = Convert .ToInt32(arylist[0]);
        Console .WriteLine(firstlist.ToString());
        //ArrayList遍歷
        int sum2 = 0;
        for (int i = 0; i < arylist.Count; i++) {
        //sum2 += Convert.ToInt32(arylist[i]);//發生拆箱操作
        Console .WriteLine(arylist[i].ToString());
        }
        foreach (object item in arylist) {
        sum2 += Convert .ToInt32(item);
        }
        Console .WriteLine(sum2);
        //ArrayList移除 只是移除 不是刪除
        arylist.Remove(1000); //移除內容是1000的 Remove移除內部的某個對象
        arylist.RemoveAt(1); //移除第二項 按索引移除
        //注意 移除元素 ArrayList數組會重新分配索引 所以移除操作最好是倒敘移除元素
        //如果移除所有的元素 直接使用Clear
        //arylist.Clear();
        if (arylist.Contains(3000)) {
        Console .WriteLine("包含" );
        }
        //ArrayList還有ToArray()但是意義不大
        //這里是在ArrayList中添加值類型 那么引用類型呢????添加Student類的對象?
        Console .Read();
        }
        }
        }

        b、ArrayList對引用類型的操作
        代碼如下:

        using System;
        using System.Collections;
        namespace _09_ArrayListObject {
        //ArrayList對引用類型的操作
        class Student {
        public Student(string name, int age) {
        this .name = name;
        this .age = age;
        }
        private string name;
        public string Name {
        get {
        return name;
        }
        set {
        name = value ;
        }
        }
        private int age;
        public int Age {
        get {
        return age;
        }
        set {
        age = value ;
        }
        }
        }
        class Program {
        static void Main( string[] args) {
        Student xyy = new Student( "小月月" , 14);
        Student fj = new Student( "鳳姐" , 18);
        Student fr = new Student( "芙蓉姐姐" , 19);
        Student xl = new Student( "犀利哥" , 20);
        ArrayList student = new ArrayList();
        student.Add(xyy); //添加 也可以使用AddRange
        student.Add(fj);
        student.Add(fr);
        student.Add(xl);
        //移除
        //student.Remove(fj);//這里移除的就是對象 而不是值
        //student.RemoveAt(1);//索引移除
        //移除不掉fj 因為Remove后是object 按索引移除
        //Student stu = new Student("鳳姐", 18);
        //student.Remove(stu);
        //Console.WriteLine(student.Contains(stu));//false 通過索引檢索 因為stu與fj地址是不一樣的
        //遍歷
        for (int i = 0; i < student.Count; i++) {
        Student s = student[i] as Student; //因為添加前發生裝箱操作 所以 現在需要拆箱 student[i]是不能點出name的
        Console .WriteLine(s.Name);
        }
        ArrayList ary = new ArrayList();
        ary.Add( "鳳姐" );
        ary.Add( "小月月" );
        //string類同樣是引用類型 但是這里有些特別
        string name = "鳳姐" ;
        Console .WriteLine(ary.Contains(name));//string比較的是內容 所以返回true
        //根據學生姓名 獲取學生對象 雖然ArrayList可以實現 但是相當的復雜 而且效率低下 所以接下來學習HashTable
        Console .Read();
        }
        }
        }

        c、HashTable
        代碼如下:

        using System;
        using System.Collections;
        namespace _10_HashTable {
        class Student {
        public Student(string name, int age) {
        this .name = name;
        this .age = age;
        }
        private string name;
        public string Name {
        get {
        return name;
        }
        set {
        name = value ;
        }
        }
        private int age;
        public int Age {
        get {
        return age;
        }
        set {
        age = value ;
        }
        }
        }
        class Program {
        static void Main( string[] args) {
        //仍然使用Student的類來實現
        //Hashtable鍵值對形式 key value 相當于字典 能根據學生的姓名快速的找到對象
        Student xyy = new Student( "小月月" , 14);
        Student fj = new Student( "鳳姐" , 18);
        Student fr = new Student( "芙蓉姐姐" , 19);
        Student xl = new Student( "犀利哥" , 20);
        Hashtable student = new Hashtable();
        student.Add( "小月月" , xyy);
        student.Add( "鳳姐" , fj);
        student.Add( "芙蓉姐姐" , fr);
        student.Add( "犀利哥" , xl);
        //student.Add("犀利哥",xl);//錯誤 字典中的關鍵字key 不允許重復 所以不能再添加犀利哥
        //移除 因為沒有索引 所以沒有RemoveAt()
        student.Remove( "小月月" );//根據key來移除
        student.Clear();
        student.ContainsKey( "鳳姐" );//判斷是不是含有這個鍵
        //遍歷 因為字典沒有索引 所以不能使用for來遍歷 只能使用foreach
        //按key遍歷 經常用
        foreach (object key in student.Keys) {
        Student stu = student[key] as Student;
        Console .WriteLine(key);
        Console .WriteLine(stu.Age);
        }
        //按value遍歷
        foreach (object value in student.Values) {
        Student stu = value as Student;
        if (stu != null ) {
        Console .WriteLine(stu.Age);
        }
        }
        //如果不按key 也不按value遍歷 對字典遍歷就是對字典的鍵值對進行遍歷
        foreach (DictionaryEntry de in student) {
        Console .WriteLine(de.Key);
        Student s = de.Value as Student; //因為得到的是object類型 所以 還需要轉換才可以使用
        Console .WriteLine(s.Age);
        }
        Student s2 = student["小月月" ] as Student ;//通過姓名找到該對象 獲取其他的屬性
        if (s2 != null ) {
        Console .WriteLine(s2.Age);
        }
        Console .Read();
        }
        }
        }

        d、練習
        代碼如下:

        using System;
        using System.Collections;
        namespace _11_ArrayList練習 {
        class Program {
        //還是那句話 理解題目之后 有了思路再開始寫code 思路最重要
        static void Main( string[] args) {
        //兩個集合{ “a”,“b”,“c”,“d”,“e”}和{ “d”, “e”, “f”, “g”, “h” },把這兩個集合去除重復項合并成一個
        ArrayList ary1 = new ArrayList { "a" , "b" , "c", "d" , "e" };
        ArrayList ary2 = new ArrayList { "d" , "e" , "f", "g" , "h" };
        //遍歷兩個集合
        for (int i = 0; i < ary2.Count; i++) { //循環遍歷ary2元素與ary1逐個比較 如果存在相同值 則不添加 否則追加到ary1中
        if (!ary1.Contains(ary2[i])) {//有Contains方法 如果沒有 不知道有多復雜
        ary1.Add(ary2[i]);
        }
        }
        foreach (object item in ary1) {
        Console .Write(item);
        }
        //隨機生成10個1-100之間的數放到ArrayList中,要求這10個數不能重復,并且都是偶數
        ArrayList arylist = new ArrayList();
        //int numCount = 0;
        while (true ) {
        Random ran = new Random();
        int num = ran.Next(1, 100);
        if (num % 2 == 0 && !arylist.Contains(num)) { //添加!arylist.Contains(num)這句話 解決以下問題
        arylist.Add(num); //為什么直接運行總顯示第一個滿足條件數值 而單步調試卻顯示正確結果???
        }
        if (arylist.Count == 10) {
        break ;
        }
        }
        foreach (object item in arylist) {
        Console .WriteLine(item);
        }
        //有一個字符串是用空格分隔的一系列整數,寫一個程序把其中的整數做如下重新排列打印出來:奇數顯示在左側、偶數顯示在右側。比如‘2 7 8 3 22 9'顯示成‘7 3 9 2 8 22
        string str = "2 7 8 3 22 9" ;
        ArrayList ary3 = new ArrayList();
        ArrayList ary4 = new ArrayList();
        string [] s = str.Split(' ' );
        foreach (var item in s) {
        if (Convert .ToInt32(item) % 2 == 0) {
        ary4.Add(item);
        } else {
        ary3.Add(item);
        }
        }
        ary3.AddRange(ary4); //因為ary1類型為object 所以無法使用string類的join方法實現字符拼接 后面學過泛型集合可以處理
        string newstr = ary3[0].ToString();//簡單方式去掉空格
        for (int i = 1; i < ary3.Count; i++) {
        newstr += " " + ary3[i];
        }
        Console .WriteLine("原字符串:{0},篩選后的字符串{1}" , str, newstr + "test" );
        Console .Read();
        }
        }
        }

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        集合類Array List HashTable實例操作練習

        集合類Array List HashTable實例操作練習:集合常用操作添加、遍歷、移除 命名空間System.Collections ArrayList 可變長度數組,使用類似于數組 屬性 Capacity Count 方法 Add() AddRange() Remove() RemoveAt() Clear() Contains() ToArray() Hashtable 鍵值
        推薦度:
        標簽: 集合 arraylist list
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲中文字幕日产乱码高清app | 成人免费ā片在线观看| 四虎在线免费播放| 免费二级毛片免费完整视频| 亚洲成熟丰满熟妇高潮XXXXX | 国产免费小视频在线观看| 亚洲综合无码精品一区二区三区| 农村寡妇一级毛片免费看视频| 中文字幕免费在线| 亚洲一区二区三区乱码A| 亚洲综合激情九月婷婷| 日韩成人毛片高清视频免费看| 午夜国产大片免费观看| 免费的黄色的网站| 伊人久久大香线蕉亚洲五月天| 成人免费av一区二区三区| 777亚洲精品乱码久久久久久 | 久久91亚洲精品中文字幕| 亚洲av日韩专区在线观看| 成人亚洲综合天堂| 久久亚洲精品国产亚洲老地址 | 久久亚洲国产精品五月天婷| 在线精品亚洲一区二区 | 99久久国产精品免费一区二区| 亚洲国产综合专区在线电影| 亚洲av无码无线在线观看| 亚洲黄黄黄网站在线观看| 中文字幕乱码一区二区免费| 亚洲欧洲另类春色校园小说| 日本免费久久久久久久网站| 久久久久国产成人精品亚洲午夜| 人人揉揉香蕉大免费不卡| 亚洲熟伦熟女专区hd高清| 精品国产亚洲男女在线线电影 | 成人免费视频77777| 免费看黄网站在线看| 亚洲AV日韩精品久久久久久| 最近中文字幕mv手机免费高清| 亚洲日韩中文字幕天堂不卡| 国产一区二区三区免费视频| 免费一级毛片无毒不卡|