Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 客户词典索引_C#_Dictionary_User Defined - Fatal编程技术网

C# 客户词典索引

C# 客户词典索引,c#,dictionary,user-defined,C#,Dictionary,User Defined,我有一个名为ClusterMemberas的类: public class ClusterMember { public string _name; public ClusterMember(string _name) { this._name = _name; } public string _Name { get { return _name;

我有一个名为
ClusterMember
as的类:

public class ClusterMember
{
        public string _name;

        public ClusterMember(string _name)
        {
            this._name = _name;
        }

        public string _Name
        {
            get { return _name; }

            set { _name = value; }
        }
}

public class Cluster
{
        private Dictionary<int, List<ClusterMember>> _dic;

        public Cluster(Dictionary<int, List<ClusterMember>> _dic)
        {   
            this._dic = _dic;
        }

        public Dictionary<int, List<ClusterMember>> _Dictionary
        {
            get { return _dic; }

            set { _dic = value; }
        }

        // adding members to clusters
        public void AddToCluster(int _id, ClusterMember _clusMem)
        {
            // checks if cluster with specific id is already in Dictionary
            if (!_dic.ContainsKey(_id))
            {
                _dic.Add(_id, new List<ClusterMember>());
            }
            else
            {
                _dic[_id].Add(_clusMem);
            }
        }

        // get members count for specific cluster id
        public int GetCount(int id)
        {
            return _dic[id].Count;
        }

        // get members count for all clusters
        public Dictionary<int, int> GetCounts()
        {
            return _dic.ToDictionary(k => k.Key, v => v.Value.Count);
        }

       public void Print_Clusters(List<Cluster> _clusterToPrint, double _alpha, int _length)
       {
            Console.WriteLine("\n" + "Number of Customers = " + _length + "\n");
            Console.WriteLine("\n" + "Alpha = " + _alpha + "\n");
            Console.WriteLine("\n" + "Number of Clusters = " + _clusterToPrint.Max() + "\n");  

            foreach ( var _clusters in _clusterToPrint  )
            {
                Console.WriteLine("Cluster ID {0} - ClusterMember {1}", _clusters._Dictionary.Keys, _clusters._Dictionary.Values);
                Console.Write("\n");
            }
            Console.ReadLine();
       }
}  
我已经插入了
1
,即(
++currentTables
)作为类的
\u dic
成员的第一个索引,即
集群
,而默认情况下,它应该以
0
开头


这是索引插入的错误吗?但是我想用
1
而不是
0
开始集群编号。如何解决此错误?

盲目运行代码会在此行引发异常:

  • \u myCluster.Count
    1
    ,表示您只能访问
    \u myCluster[0]
  • \u i
    的值为
    1
    ,因此您试图访问
    \u myCluster[1]
    ,因此出现异常
  • 错误可能是您从
    1
    开始的
    \u i
    (索引是从零开始的),可能您想要访问
    \u myCluster[i-1]
    ,只有您知道

  • 请告诉我们它发生的线路on@Rob我不能在这里提到行号,但它是这一行
    double\u probOldValue=\u myClusters[\u I].GetCount(\u I)/((u I+1)-1+\u alpha)
    你能在那条线上画一个断点,告诉我们什么是
    \u i
    以及
    \u myClusters
    有多大吗?另外,为什么您对
    \u myClusters
    GetCount
    使用相同的索引?您没有检查
    \u i
    是否小于
    \u myClusters.Count
    ,您只是对照
    \u cust.Count-1
    检查
    \u i
    。换句话说:你们正在检查你们是否有足够的苹果,但你们接着拿了一个橘子。“我知道如何调试”-若你们愿意,那个么就并没有问题了,或者问题只是列出了所有变量值的一行(以防你们怀疑框架中有bug)。目前,您对代码是否正确没有信心。为什么?因为您没有调试它来检查您所做的所有假设是否正确。如果您希望其他人进行调试,请提供。当前代码不足以调试(例如,
    \u probNewValue
    定义缺失,我们不知道如何运行您的代码,代码太长等等)。@Sinatr--您对此异常的看法是正确的,但是
    \u myCluster
    是自定义类型列表的对象,即
    列表
    和类
    集群
    中使用的索引是集群编号,而我不想使用
    0
    作为集群编号,因此引发此异常。你能帮我整理一下吗?我不知道什么是群集号,但你可以:1)更改
    \u I
    0
    开始,但显示为
    \u I+1
    以查看群集号2)保持
    \u I
    1
    开始,但访问
    \u myCluster[\u I-1]
    将群集编号转换为有效索引。顺便说一句,对于局部变量/参数,以下划线开头的名称并不酷。我自己只对字段(显然不是公共字段)使用
    ,以便能够重复使用相同的名称(但没有
    )来命名参数/局部变量。聚类数是“类
    聚类
    存储的总聚类数”,而不是
    计数
    ,它与索引无关。当
    Count==1
    时,您可以操作的最大索引为
    0
    。当
    Count==0
    时,您不能操作任何索引。我已更改为
    \u myCluster[\u I-1]
    但它给出的计数是
    0
    ,因为
    probOldValue
    的值在调试时显示为
    0.0
    ,而它应该显示
    1
    的计数,那么
    probOldValue
    的值将不会是
    0.0
    ,因为类
    cluster
    中有一个集群成员目前,即第一个客户
    public static void DP_Cluster(List<string> _cust, double _alpha)
    {
            var _customer = new List<string>();
            var _currentTables = 0;     // current number of tables i.e. "k"
    
            var _dicInitial = new Dictionary<int, List<ClusterMember>>();
            var _customerNumber = 0;    // running customer number i.e. "n"
    
            var _probOld = new Dictionary<int, double>();
            //var _probOld = new List<Double>();         // porbability to be in Old cluster
            var _probNew = 0.0;         // porbability to be in New cluster 
    
            List<Cluster> _myClusters = new List<Cluster>();
    
            Cluster _cluster = new Cluster(_dicInitial); 
            // add first customer from "_cust" list directly to a new cluster
            _cluster.AddToCluster(++_currentTables, new ClusterMember(_cust.ElementAt(_customerNumber)));
    
            _myClusters.Add(_cluster);
            _probOld.Add(_currentTables, Convert.ToDouble(1) / Convert.ToDouble(1 + _alpha));
    
            for (int _i = 1; _i < _cust.Count - 1; _i++)
            {
                if (_i <= _currentTables)
                {
                    // get customer Count in Cluster i / customerNumber + alpha
                    // Got ERROR here at "_myClusters[_i]"
                    double _probOldValue = _myClusters[_i].GetCount(_i) / ((_i + 1) - 1 + _alpha);  
    
                        _probOld.Add(_currentTables, _probOldValue);
                    }
                else if ( _i == _currentTables + 1)
                {
                    _probNew =_alpha / ((_i + 1) - 1 + _alpha);
                    _currentTables++;
                    _probOld.Add(_currentTables, _probNew);
                }
    
                List<int> _keyList = new List<int>(_probOld.Keys);
                Random _random = new Random();
                int _randomKey = _keyList[_random.Next(_keyList.Count)];
                            _cluster.AddToCluster(_randomKey, new ClusterMember(_cust.ElementAt(_i)));
            }
            _myClusters.Add(_cluster);
    
            _cluster.Print_Clusters(_myClusters, _alpha, _cust.Count);
    
    }
    public static List<string> GetRandomString(int _numOfStrings, int _stringLength)
    {
            string[] _arrStr = new string[_numOfStrings];
            List<string> _listSt;
            const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var _random = new Random();
    
            for (int _i = 0; _i < _arrStr.Length; _i++)
            {
                _arrStr[_i] = new string(Enumerable.Repeat(_chars, _stringLength).Select(s => s[_random.Next(s.Length)]).ToArray());
            }
            _listSt = new List<string>(_arrStr);
            return _listSt;
    }
    
    static int Main()
    {
          double _alfa = 5;
          int _n = 30;      
          List<string> _data = GetRandomString(_n, 8);
          DP_Cluster(_data, _alfa);
          return 0;
    }
    
    _cluster.AddToCluster(++_currentTables, new ClusterMember(_cust.ElementAt(_customerNumber)));