[英]Assigning value to a two dimensional array
这是我认为更好的数据结构的一个示例:
对于计算机,您需要保留一些信息,因此为计算机创建一个类:
public class Computer
{
public string HostName { get; set; }
public int Row { get; set; }
public int Column { get; set; }
//Whatever else you want to add, like
public bool IsActive { get; set; }
public bool IsScrewingAround { get; set; }
}
然后,您可以使用字典来存储数据:
public Dictionary<string, Computer> _computers =
new Dictionary<string, Computer>(StringComparer.OrdinalIgnoreCase);
然后向其中添加计算机,如下所示:
_computers.Add("someHostName", new Computer()
{
HostName = "someHostName",
Row = 0,
Column = 4,
IsActive = true,
IsScrewingAround = false
});
然后,您可以在O(1)操作中按主机名查找计算机,例如:
var cpu = _computers["hostName"];
或获取搞砸的计算机列表:
var screwOffs = _computers.Values.Where(c => c.IsScrewingAround);
想要第4行中的所有计算机吗?
var row4 = _computers.Values.Where(c => c.Row == 4);
这为您提供了一个优于阵列的优势,如果您的房间形状发生变化,阵列可以根据添加到其中的Computer
的数量来增长。
如果您不需要主机名的O(1)查找,那么实际上这可能是List<Computer>
,考虑到您教室中的物品数量相对较少,我猜这会很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.