濮阳杆衣贸易有限公司

主頁 > 知識庫 > 集合類Array List HashTable實例操作練習(xí)

集合類Array List HashTable實例操作練習(xí)

熱門標(biāo)簽:杭州智能電話機(jī)器人 百度地圖標(biāo)注點擊事件 地圖標(biāo)注位置多的錢 濟(jì)源人工智能電話機(jī)器人價格 山東防封電銷卡辦理套餐 怎樣在地圖標(biāo)注消火栓圖形 泰州手機(jī)外呼系統(tǒng)軟件 廈門四川外呼系統(tǒng) 內(nèi)蒙古智能電銷機(jī)器人哪家強(qiáng)
集合常用操作添加、遍歷、移除
命名空間System.Collections

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

a、ArrayList對值類型的操作
復(fù)制代碼 代碼如下:

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

b、ArrayList對引用類型的操作
復(fù)制代碼 代碼如下:

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; //因為添加前發(fā)生裝箱操作 所以 現(xiàn)在需要拆箱 student[i]是不能點出name的
Console .WriteLine(s.Name);
}
ArrayList ary = new ArrayList();
ary.Add( "鳳姐" );
ary.Add( "小月月" );
//string類同樣是引用類型 但是這里有些特別
string name = "鳳姐" ;
Console .WriteLine(ary.Contains(name));//string比較的是內(nèi)容 所以返回true
//根據(jù)學(xué)生姓名 獲取學(xué)生對象 雖然ArrayList可以實現(xiàn) 但是相當(dāng)?shù)膹?fù)雜 而且效率低下 所以接下來學(xué)習(xí)HashTable
Console .Read();
}
}
}

c、HashTable
復(fù)制代碼 代碼如下:

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的類來實現(xiàn)
//Hashtable鍵值對形式 key value 相當(dāng)于字典 能根據(jù)學(xué)生的姓名快速的找到對象
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);//錯誤 字典中的關(guān)鍵字key 不允許重復(fù) 所以不能再添加犀利哥
//移除 因為沒有索引 所以沒有RemoveAt()
student.Remove( "小月月" );//根據(jù)key來移除
student.Clear();
student.ContainsKey( "鳳姐" );//判斷是不是含有這個鍵
//遍歷 因為字典沒有索引 所以不能使用for來遍歷 只能使用foreach
//按key遍歷 經(jīng)常用
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遍歷 對字典遍歷就是對字典的鍵值對進(jìn)行遍歷
foreach (DictionaryEntry de in student) {
Console .WriteLine(de.Key);
Student s = de.Value as Student; //因為得到的是object類型 所以 還需要轉(zhuǎn)換才可以使用
Console .WriteLine(s.Age);
}
Student s2 = student["小月月" ] as Student ;//通過姓名找到該對象 獲取其他的屬性
if (s2 != null ) {
Console .WriteLine(s2.Age);
}
Console .Read();
}
}
}

d、練習(xí)
復(fù)制代碼 代碼如下:

using System;
using System.Collections;
namespace _11_ArrayList練習(xí) {
class Program {
//還是那句話 理解題目之后 有了思路再開始寫code 思路最重要
static void Main( string[] args) {
//兩個集合{ “a”,“b”,“c”,“d”,“e”}和{ “d”, “e”, “f”, “g”, “h” },把這兩個集合去除重復(fù)項合并成一個
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++) { //循環(huán)遍歷ary2元素與ary1逐個比較 如果存在相同值 則不添加 否則追加到ary1中
if (!ary1.Contains(ary2[i])) {//有Contains方法 如果沒有 不知道有多復(fù)雜
ary1.Add(ary2[i]);
}
}
foreach (object item in ary1) {
Console .Write(item);
}
//隨機(jī)生成10個1-100之間的數(shù)放到ArrayList中,要求這10個數(shù)不能重復(fù),并且都是偶數(shù)
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); //為什么直接運行總顯示第一個滿足條件數(shù)值 而單步調(diào)試卻顯示正確結(jié)果???
}
if (arylist.Count == 10) {
break ;
}
}
foreach (object item in arylist) {
Console .WriteLine(item);
}
//有一個字符串是用空格分隔的一系列整數(shù),寫一個程序把其中的整數(shù)做如下重新排列打印出來:奇數(shù)顯示在左側(cè)、偶數(shù)顯示在右側(cè)。比如‘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方法實現(xiàn)字符拼接 后面學(xué)過泛型集合可以處理
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();
}
}
}
您可能感興趣的文章:
  • Java中的collection集合類型總結(jié)
  • 集合類List與Dictonary實例練習(xí)
  • js正則函數(shù)match、exec、test、search、replace、split使用介紹集合
  • SQL集合函數(shù)中case when then 使用技巧
  • 使用Enumeration和Iterator遍歷集合類詳解
  • Python中集合類型(set)學(xué)習(xí)小結(jié)
  • Python set集合類型操作總結(jié)
  • C#中DataSet轉(zhuǎn)化為實體集合類的方法
  • Swift教程之集合類型詳解
  • C++簡單集合類的實現(xiàn)方法

標(biāo)簽:臺州 百色 朝陽 新鄉(xiāng) 喀什 朔州 周口 洛陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《集合類Array List HashTable實例操作練習(xí)》,本文關(guān)鍵詞  集合,類,Array,List,HashTable,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《集合類Array List HashTable實例操作練習(xí)》相關(guān)的同類信息!
  • 本頁收集關(guān)于集合類Array List HashTable實例操作練習(xí)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    喀喇沁旗| 宁阳县| 旬邑县| 双峰县| 信阳市| 民县| 太和县| 新安县| 手游| 兰西县| 习水县| 保靖县| 修水县| 邢台市| 社旗县| 化隆| 泰宁县| 金堂县| 溧阳市| 安义县| 手游| 友谊县| 绥滨县| 五家渠市| 宁津县| 万宁市| 石柱| 报价| 星座| 锦屏县| 青田县| 阿巴嘎旗| 东辽县| 桐梓县| 襄汾县| 勐海县| 洞头县| 吉木萨尔县| 桓台县| 高雄市| 开鲁县|