我必须在任何数据结构中存储两种类型的信息,以便我在C#中提出二维数组的废弃解决方案。我必须存储为:
int
数据类型int
数据类型中的群集成员数
如果我使用2D数组:
Int32[,] _clusterMembership = new Int32[10, 10];
但这里的问题是:
所以问题是: 如何设法将此信息存储在C#中?
附录
我必须在这个方法中使用这个问题的答案:
public static List<Cluster> DP_Cluster(List<string> _customer, double _alpha)
{
var _currentClusters = 0; // current number of clusters i.e. "k"
var _memberNumber = 0; // running member number i.e. "n"
//var _dic = new Dictionary<int, List<string>>();
var _probOld = 0.0;
var _probNew = 0.0;
List<Cluster> myClusters = new List<Cluster>();
Cluster cluster = new Cluster(_currentClusters += 1);
cluster.Members.Add(new ClusterMember { Name = _customer.ElementAt(_memberNumber) });
myClusters.Add(cluster);
//_dic.Add(_currentClusters, _customer.ElementAt(_memberNumber));
_currentClusters += 1;
for(int _i = 1; _i < _customer.Count - 1; _i++)
{
if( _i <= _currentClusters)
{
_probOld = myClusters[_i].Members.Count / ((_i+1) - 1 + _alpha);
}
else
{
_probNew = _alpha / ((_i+1) - 1 + _alpha);
}
if(_probNew > _probOld)
{
// Add _customer.ElementAt(_memberNumber+=1) to New Cluster
Cluster cluster = new Cluster( _currentClusters += 1 ); // Here is an error as we defining same name for another cluster
myClusters.Add(cluster);
}
else
{
// Add _customer.ElementAt(_memberNumber+=1) to Old Cluster
}
}
return myClusters;
}
答案 0 :(得分:2)
正如已经提到的那样。您只需使用一个列表即可。底部代码示例显示了如何创建所需的列表类型以及如何在该列表中添加和访问值。
using System.IO;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
//Creating a list of lists that contains integers
List<List<int>> clusters = new List<List<int>>();
//each list in the above list consists of a list of integers. So we need to add list of integers to that list
List<int> row = new List<int>();
//now we add integers to the list
row.Add(1); row.Add(2); row.Add(3); row.Add(4);
//Now we add the list of integers to the list of lists of integers
clusters.Add(row);
foreach(List<int> rows in clusters)
{
foreach(int num in rows)
{
System.Console.WriteLine(num);
}
}
Console.WriteLine("number of rows: {0}", clusters.Count);
Console.WriteLine("number of elements in the first row: {0}", clusters[0].Count);
}
}
答案 1 :(得分:1)
您应该考虑制作两种类型,一种用于群集,另一种用于成员:
<强>成员强>
public class ClusterMember
{
public string Name {get;set;}
// more properties...
}
<强>集群强>
public class Cluster
{
public int ID {get;}
public List<ClusterMember> Members {get;}
public Cluster(int id)
{
ID = id;
Members = new List<ClusterMember();
}
}
然后您可以将群集存储在列表中
List<Cluster> myClusters = new List<Cluster>();
Cluster cluster = new Cluster(1);
cluster.Members.Add(new ClusterMember { Name = "Member1" });
myClusters.Add(cluster);
更新我认为您希望对数据做更多的工作,而不仅仅是存储这两个信息,并尝试提供更好的面向对象的方法。
获取您的计数:
int totalNumberOfClusters = myClusters.Count;
int numberOfMembersInOneCluster = cluster.Members.Count;
int totalNumberOfClusterMembers = myClusters.Sum(c => c.Members.Count);
并输出每个群集的成员数量:
foreach(Cluster c in myClusters)
Console.WriteLine($"Cluster {c.ID} has {c.Members.Count} members.");
答案 2 :(得分:0)
您可以考虑使用列表列表;
List<List<int>> clusters;
有关更多信息以及如何将其变为更通用的课程,请参阅此问题的另一个问题:https://stackoverflow.com/a/1596563/6065552
答案 3 :(得分:0)
您可以将数据存储在包含keyvaluepair项目的列表中。 或者使用词典
SELECT image_id
FROM list
WHERE style_id IN (24, 25, 27)
GROUP BY image_id
HAVING COUNT(DISTINCT style_id) = 3
因此您可以为每个群集添加新的keyvaluepair。
答案 4 :(得分:0)
static void Main(string[] args)
{
var clusterMembership = new Dictionary<int, int>();
//Add cluster 123 and assign a member count of 4
clusterMembership.Add(123, 4);
//Change member count for cluster 123 to 5
clusterMembership[123] = 5;
//Remove cluster 123
clusterMembership.Remove(123);
//Get the number of clusters in the dictionary
var count = clusterMembership.Count;
//Iterate through the dictionary
foreach(var clusterKey in clusterMembership.Keys)
{
var memberCount = clusterMembership[clusterKey];
}
}