ArrayList[集合类]、遍历Hsahtable【怎么搞的今天网是有了,怎么老断电呢~!本来好好的心情学习着,破电给人添堵】

9.10  敬爱的老师们:节日快乐~!

ArrayList[集合类]:可以添加、移除、遍历;可变长度,用法和数组类似
数组特点:1.类型统一、2.长度固定
属性:count(得到集合中的实际存放数据的个数)
方法:Add()添加   remove()移除  RemoveAt()遍历  ToArry()
引用快捷键:ctrl+Alt+F10

ArrayList[集合类]的建立、添加:
ArrayList arrayList=new ArrayList();
Console.WriteLine(arrayList.Count);
arrayList.Add(10);
arrayList.Add("HELLO WORLD");
arrayList.Add(3.45);
Console.WriteLine(arrayList.Count);//元素实际个数集合中
Console.WriteLine(arrayList.Capacity);
Console.ReadKey();

ArrayList[集合类]移除:
arrayList.Remove(3.45);

ArrayList[集合类]遍历:
for(int i=0;i<9;i++)
{
arrayList.RemoveAt(0);
}
arrayList.TrimToSize();//收缩

Hashtable[健值对的集合]
Hashtable hashtable=new Hashtable();//Hashtable解析
//定义Person实例
Person YQQ=new Person(){Name="杨巧巧"};
hash.Add(YQQ.Name,YQQ);

Person Yy=new Person(){Name="尤越"};
hash.Add(Yy.Name,Yy);

//取值
Console.WriteLine( ((Person) hash["杨巧巧"]).Name);
方法:Add(object key,object value);
hash["key"]="修改";
Remove("key");

#region
程序运行时不运行
#endregion

遍历查找

for (int i = 0; i < arrInt.Length; i++)
{
 if (arrInt[i] == number)
  {
    b = true;
  }

}
if (b)
{
 Console.WriteLine("存在!");
 }
 else
{
 Console.WriteLine("不存在!");
 }

//健的地址是算出来的,不是遍历得到得,所以才效率高
int[] arrInt={25,125,225,325,425,525,625};
int number=325;
bool b=false;
int index=number/100;
if(index<arrInt.Length)
{
Console.WriteLine("可能存在!");
Console.WriteLine(arrInt[index]);
}
else
{
Console.WriteLine("not exist");
}
Console.ReadKey();

if(hash.ContainsKey("张兰"))
{
Console.WriteLine("包含");
}
else
{
Console.WriteLine("不包含");
}

遍历Hsahtable
Hsahtable table=new Hsahtable();
table.Add("qq","YangQiaoQiao");
table.Add("lxf","LiuXiaoFei");
table.Add("zl","ZhangLan");
foreach(DictionaryEntry item in table)
{//DictionaryEntry是个结构体
Console.WriteLine(item.key+"  "+item.Value);
}
foreach(var item in table.Keys)
{
Console.WriteLine(itme);
}
foreach(var item in table.Value)
{
Console.WriteLine(itme);
}
//var 当变量没有赋值时、变量可能是任意一种类型;一旦给变量赋了值

,变量就成了值得类型。

1.把两个(ArrayList)集合{"a","b","c","d"}和{"c","f","d","h"}, 

合并这两个集合并除去重复项。
ArrayList arrayList1=new ArrayList(){"a","b","c","d"};
ArrayList arrayList2=new ArrayList(){"c","f","d","h"};
ArrayList arrayList3=new ArrayList();

arrayList3.AddRange(arryList1);
for(int i=0;i<arrayList2.count;i++)
{
if(!arrayList3.Contains(arrayList2[i]))
{
arrayList3.AddRange(arryList2[i]);
}
}
for(int i=0;i<arrayList3.Count;i++)
{
Console.WriteLine(arrayList3[i]);
}

2.随机生成10个1-100之间的数,放到ArrayList中,要求这10个数不能重

复,并且都是偶数。添加十次,但是会循环很多次。
Random random = new Random();
random.Next(1, 101);

ArrayList arrayRandom = new ArrayList();         
 Random random = new Random();
 while (arrayRandom.Count < 10)
{
 int ra= random.Next(1, 101);
 if (ra % 2 == 0)
{
if( !arrayRandom.Contains(ra))
{
 arrayRandom.Add(ra);
                     
 }
               
  }
 }
 Console.ReadKey();

 

//计数器看看 while循环了多少次?
int index=0
ArrayList arrayRandom = new ArrayList();         
 Random random = new Random();//写在循环里就会有重复产生延迟
 while (arrayRandom.Count < 10)
{
 int ra= random.Next(1, 101);
 if (ra % 2 == 0)
{
if( !arrayRandom.Contains(ra))
{
 index++;
 arrayRandom.Add(ra);
                     
 }
               
  }
 }
for(int i=0;i<arrayRandom.Count;i++)
{
Console.WriteLine(arrayRandom[i]);
}
Console.WriteLine("一共循环了:{0}次",index);
 Console.ReadKey();
3.有一个字符串是用空格分隔的一系列整数,写一个程序把其中的,奇数排列在前偶数排列在后。2 4 7 9 6 8 5————7 9 5 2 4 6 8

string msg="2 4 7 9 6 8 5";
string[] nums=msg.Split('');
//结果7 9 5 2 4 6 8
//存放奇数
ArrayList listEven=new ArrayList();
//存放偶数
ArrayList listEven=new ArrayList();
//遍历数组
for(int i=0;i<nums.Length;i++)
{
if(convert.ToInt32(nums[i])%2!=0)
{
listOdd.Add(nums[i]);
}
else
{
listEven.Add(nums[i]);
}

}
listOdd.AddRange(listEven);
StringBuilder sd=new StringBuilder();//StringBuilder字符串构造定义字符串
for(int i=0;i<listOdd.Count;i++)
{
sd.Append(list[i]+"");
}
Console.WriteLine(sb);
 Console.ReadKey();

4.做一个game————“猜拳”( winfor,控制台版本都要)

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //玩家出剪刀
        private void btnJiandao_Click(object sender, EventArgs e)
        {
         //获取当前简单按钮的文本内容“剪刀”
          string strJiandao = btnJiandao.Text; 
            //将按钮上的文本 赋给label框lblPlayer 
           lblPlayer.Text = strJiandao; 
            // 调用下面 将文本转换成相应整型的方法 将文本框的内容 剪刀转换成对应的整数 
            //并赋给 vPlayer 
            int vPlayer = changtoValue(strJiandao); 
          // MessageBox.Show(v.ToString()); 
          //运行PC的方法 电脑随机出拳   
            PC(); 
           //将电脑label框 lblPC的内容赋给字符串strPC 
            string strPC = lblPC.Text; 
           //调用转成字符串为整数的方法 将相应的电脑出拳转成整数 
           int vPC = changtoValue(strPC); 
            //MessageBox.Show("计算机出了值为:"+ vPC.ToString()); 
            //调用裁判的方法 传入2个整数值 玩家 和电脑的数据 进行对比 
            Judge(vPlayer, vPC); 
        }
        //如上同理
        //玩家 出石头
        private void btnShitou_Click(object sender, EventArgs e)
        {
            string strShitou = btnShitou.Text; 
           lblPlayer.Text = strShitou; 
           int vPlayer = changtoValue(strShitou); 
           // MessageBox.Show(v.ToString()); 
            PC(); 
            string strPC = lblPC.Text; 
            int vPC = changtoValue(strPC); 
            //MessageBox.Show("计算机出了值为:" + vPC.ToString()); 
           Judge(vPlayer, vPC); 

        }
        //如上同理 
        //玩家出布 
        private void btnBu_Click(object sender, EventArgs e)
        {
            string strBu = btnBu.Text; 
            lblPlayer.Text = strBu; 
            int vPlayer = changtoValue(strBu); 
          //MessageBox.Show(v.ToString()); 
           PC(); 
           string strPC = lblPC.Text; 
          int vPC = changtoValue(strPC); 
          //MessageBox.Show("计算机出了值为:" + vPC.ToString()); 
           Judge(vPlayer, vPC); 

        }


        //写一个将鼠标点击事件 将字符串转换成相应数值的方法 
        public int changtoValue(string str) 
        { 
            //new一个整型n 
            int n = 0; 
           //对传入的字符串进行判断 
            //如果 为剪刀 则给n 赋予值1 
            if (str == "剪刀") 
            { 
                n = 1; 
            } 
            //同理 
            else if (str == "石头") 
            { 
                n = 2; 
            } 
            //同理 
            else if (str == "布") 
            { 
                n = 3; 
            } 
           else 
            { 
               throw new Exception("未知错误"); 
            } 
            //最后返回一个转换成功的整数n 
            return n; 
       } 
 
        
        // 剪刀1  石头2  布3 
        //写一个电脑随机出拳的方法 
        public string PC() 
        { 
            //用一个随机函数 电脑获取的数为1-3 
            Random ran = new Random(); 
            //random.Next方法 取最小值,但不取最大值, 所以在1-3内随机取值 
            int n = ran.Next(1, 4); 
            //new 一个空字符串 strPC 
            string strPC = ""; 
            //进行简单判定 获取的随机值 同时转换成相应的字符串 
            //如果随机值取到1 
            if (n == 1) 
            { 
                //则赋给空字符串一个 "剪刀" 
                strPC = "剪刀"; 
            } 
           //同理 
           else if (n == 2) 
           { 
                strPC = "石头"; 
            } 
            //同理 
           else if (n == 3) 
            { 
                strPC = "布"; 
            } 
            //如果随机值不在1-3范围内 抛出一个异常 
            else 
            { 
                throw new Exception("未知错误!"); 
            } 
            //将获取的字符串 赋给label框 lblPC 
            lblPC.Text = strPC; 
              //并返回一个字符串 strPC 
           return strPC; 
 
        } 
 
       //比较玩家和电脑的值 
        public void Judge(int Player, int PC) 
        { 
            // 剪刀1  石头2  布3 
            //赢的情况有2种 1种是 玩家比电脑值大1  一种是小2 
            if (Player - PC == 1||Player - PC==-2) 
            { 
                lblResult.Text = "你赢了"; 
            } 
            //平的情况只有1种 即为出拳相同 
           else if (Player - PC == 0) 
            { 
                lblResult.Text = "平局"; 
            } 
           //除此之外 只有输的情况了 
           else 
            { 
                lblResult.Text = "你输了"; 
           } 
        } 

    }
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值