zoukankan      html  css  js  c++  java
  • 键与值的方法实现一键多值的类

    今天模拟了一个主键带有二个值的类,也是为了对C#数据结构的加深,其实用HashTable通过合拼字符串传进去也可以,出来时再拆分并且效率更高,不过我这个是反过来,分散传进来,合拼传出来,完全是为了让大家知道,C#数据结构的重要性,不学之前你只会用ArrayList, HashTable,但你学过后,你就知道他们实现的原理.
        public class ThreeHashTable
        {
            
    private object[] Key;     //键值
            private object[] Value1;  //值1
            private object[] Value2;  //值2
            private long maxLength;       //最大元素个数
            private long length;          //已经占用的长度

            
    public ThreeHashTable()
                : 
    this(32)
            { }

            
    /// <summary>
            
    /// 构造ThreeHashTable
            
    /// </summary>
            
    /// <param name="capacity"></param>
            public ThreeHashTable(long capacity)
            {
                
    this.length = -1;      //初始时赋值为-1
                this.maxLength = capacity;    //设置最大容量
                this.Key = new object[capacity];
                
    this.Value1 = new object[capacity];
                
    this.Value2 = new object[capacity];
            }

            
    /// <summary>
            
    /// 取最大长度
            
    /// </summary>
            public long Length
            {
                
    get { return (this.length + 1); }
            }

            
    /// <summary>
            
    /// 返回或设置ThreeHashTable的最大元素个数
            
    /// </summary>
            public long MaxLength
            {
                
    get 
                {
                    
    return this.maxLength; 
                }
            }

            
    /// <summary>
            
    /// 是否已经填满,满返回true,否则返回false
            
    /// </summary>
            
    /// <returns></returns>
            public bool Full()
            {
                
    return ((this.length + 1>= this.maxLength);  //length+1不为最大会值时就还没有填满,因为length初始化是-1,而且是从0开始
            }

            
    /// <summary>
            
    /// 是否为空,空返回true,否则返回false
            
    /// </summary>
            
    /// <returns></returns>
            public bool Empty()
            {
                
    return (this.length == -1);   //当没有元素存在,length为-1
            }

            
    /// <summary>
            
    /// 是否存在键值,存在返回true,否则返回false
            
    /// </summary>
            
    /// <param name="key"></param>
            
    /// <returns></returns>
            private bool ExistsKey(object key)
            {
                
    for (long i = 0; i <= this.length; i++)
                {
                    
    if (key.ToString() == this.Key[i].ToString())
                    {
                        
    return true;
                    }
                }
                
    return false;
            }

            
    /// <summary>
            
    /// 插入值到ThreeHashTable
            
    /// </summary>
            
    /// <param name="key">Object Type</param>
            
    /// <param name="value1">Object Type</param>
            
    /// <param name="value2">Object Type</param>
            public void Add(object key, object value1, object value2)
            {
                
    if (Full())
                {
                    
    throw new ArgumentOutOfRangeException("ThreeHashTable已经满员,请扩大容量");
                }
                
    if (key == null || value1 == null || value2 == null)
                {
                    
    throw new ArgumentOutOfRangeException("key或值为null或还没有初始化");
                }
                
    if (!ExistsKey(key))
                {
                    
    if (this.Key == null)
                        key 
    = new object[this.maxLength];
                    
    if (this.Value1 == null)
                        Value1 
    = new object[this.maxLength];
                    
    if (this.Value2 == null)
                        Value2 
    = new object[this.maxLength];

                    
    this.Key[++length] = key;
                    
    this.Value1[length] = value1;
                    
    this.Value2[length] = value2;
                }
            }

            
    /// <summary>
            
    /// 清除ThreeHashTable所有元素
            
    /// </summary>
            public void Clear()
            {
                
    this.length = -1;
                
    this.Key = null;
                
    this.Value1 = null;
                
    this.Value2 = null;
            }

            
    /// <summary>
            
    /// 返回key所在的位置
            
    /// </summary>
            
    /// <param name="key"></param>
            
    /// <returns></returns>
            private long KeyIndex(object key)
            {
                
    long index = 0;
                
    for (long i = 0; i < this.length; i++)
                {
                    
    if (key.ToString() == this.Key[i].ToString())
                    {
                        index 
    = i;
                        
    break;
                    }
                }
                
    return index;
            }

            
    /// <summary>
            
    /// 通过key来取得值,返回object数组
            
    /// </summary>
            
    /// <param name="key">ThreeHashTable项的key</param>
            
    /// <param name="value">返回一个包含值的数组,得到数组后,用value[0]和value[1]取值</param>
            
    /// <returns></returns>
            public object[] GetKeyValue(object key, object[] value)
            {            
                
    if (Empty())
                {
                    
    throw new ArgumentOutOfRangeException("ThreeHashTable没有填充元素为空");
                }
                
    if (!ExistsKey(key))
                {
                    
    throw new ArgumentOutOfRangeException("键值不存在");
                }
                
    else
                {
                    value 
    = new object[2];
                    
    long index = KeyIndex(key);
                    value[
    0= this.Value1[index];
                    value[
    1= this.Value2[index];
                }
                
    return value;
            }

            
    /// <summary>
            
    /// 通过key来取得值,返回以传进line隔开的字符串
            
    /// </summary>
            
    /// <param name="key"></param>
            
    /// <param name="line">这个参数很重要,是区分二个值的分隔符,不能最好能用多几个特殊组合字符串</param>
            
    /// <returns></returns>
            public string GetKeyValue(object key,string line)
            {
                
    string value = "";
                
    if (Empty())
                {
                    
    throw new ArgumentOutOfRangeException("ThreeHashTable没有填充元素为空");
                }
                
    if (!ExistsKey(key))
                {
                    
    throw new ArgumentOutOfRangeException("键值不存在");
                }
                
    else
                {
                    
    long index = KeyIndex(key);
                    value 
    = this.Value1[index].ToString() + line + this.Value2[index].ToString();
                }
                
    return value;
            }

            
    /// <summary>
            
    /// 从ThreeHashTable移除一个项
            
    /// </summary>
            
    /// <param name="key"></param>
            public void ReMove(object key)
            {
                
    if (Empty())
                {
                    
    throw new ArgumentOutOfRangeException("ThreeHashTable没有填充元素为空");
                }

                
    if (!ExistsKey(key))
                {
                    
    throw new ArgumentOutOfRangeException("键值不存在");
                }
                
    else
                {
                    
    long index = KeyIndex(key);
                    
    this.Key[index] = null;
                    
    this.Value1[index] = null;
                    
    this.Value2[index] = null;
                }
            }
        }

    测试代码:

    try
                { 
                    ThreeHashTable tht 
    = new ThreeHashTable(10);
                    Console.WriteLine(
    "-------初始化各参数列表-------");
                    Console.WriteLine(
    "现在已经有元素个数:" + tht.Length);
                    Console.WriteLine(
    "元素总数最大值:" + tht.MaxLength);
                    Console.WriteLine(
    "是否为空:" + tht.Empty().ToString());
                    Console.WriteLine(
    "是否填满:" + tht.Full().ToString());

                    
    for (int i = 0; i < 10; i++)
                    {
                        tht.Add(i, 
    "B" + i.ToString(), "C" + i.ToString());
                    }
                    Console.WriteLine(
    "-------添加元素参数列表-------");
                    Console.WriteLine(
    "现在已经有元素个数:" + tht.Length);
                    Console.WriteLine(
    "元素总数最大值:" + tht.MaxLength);
                    Console.WriteLine(
    "是否为空:" + tht.Empty().ToString());
                    Console.WriteLine(
    "是否填满:" + tht.Full().ToString());
                    Console.WriteLine(
    "-------取值方法一返回数组-------");
                    
    object[] a = new object[2];
                    a 
    = tht.GetKeyValue("1", a);
                    
    object value1 = a[0];
                    
    object value2 = a[1];
                    Console.WriteLine(
    "value1:  " + value1);
                    Console.WriteLine(
    "value2:  " + value2);
                    Console.WriteLine(
    "-------取值方法二返回字符串-------");
                    
    string value = tht.GetKeyValue("1""|");
                    Console.WriteLine(
    "value:  " + value);
                    Console.WriteLine(
    "-------移除一个元素-------");
                    tht.ReMove(
    1);
                    tht.Clear();
                    Console.WriteLine(
    "-------清除所有元素-------");
                    Console.WriteLine(
    "现在已经有元素个数:" + tht.Length);
                    Console.WriteLine(
    "元素总数最大值:" + tht.MaxLength);
                    Console.WriteLine(
    "是否为空:" + tht.Empty().ToString());
                    Console.WriteLine(
    "是否填满:" + tht.Full().ToString());
                    Console.ReadLine();
                }
                
    catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                }

    结果:
  • 相关阅读:
    flutter添加启动图及设置启动时间
    flutter中通过循环渲染组件
    flutter学习资料汇总
    flutter中显现登录页面成功后跳转的方法
    flutter 常用视图组件
    mpvue学习笔记
    按钮放大动画效果
    一位练习时长两年半的内网渗透练习生
    Kali系统中20个超好用黑客渗透工具,你知道几个?
    渗透测试之三内网跳板
  • 原文地址:https://www.cnblogs.com/whtydn/p/1538214.html
Copyright © 2011-2022 走看看