我创建了一个名为employees的类来保存员工信息.该课程如下所示.
class Employee
{
private int employeeID;
private string firstName;
private string lastName;
private bool eligibleOT;
private int positionID;
private string positionName;
private ArrayList arrPhone;
private ArrayList arrSector;
Run Code Online (Sandbox Code Playgroud)
如你所见,我创建了一个名为arrSector的数组.它采用员工关联的部门的名称.现在我也想把行业ID和行业名称一起考虑.
我的问题是如何在单个arraylist变量中实现扇区ID和扇区名称.
我想将扇区ID的值和扇区名称一起存储.任何帮助表示赞赏.
Jas*_*yon 14
创建一个对象来保存两条信息.
public class Sector
{
public string Name { get; set; }
public int Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后使用通用List而不是ArrayList.
class Employee
{
private int employeeID;
private string firstName;
private string lastName;
private bool eligibleOT;
private int positionID;
private string positionName;
private ArrayList arrPhone;
private List<Sector> arrSector;
}
Run Code Online (Sandbox Code Playgroud)
首先:ArrayList如果您可以提供帮助,请不要使用,至少如果您使用的是.NET 2或更高版本.您可以使用List<T>特定于您放入其中的类型的泛型,这可以为您节省大量的时间.
至于你的问题,你可能想要一个Hashtable或Dictionary<TKey, TValue>.Hashtables是存储一个值(键)与另一个值(值)的关联的集合.在您的情况下,您可能有一个整数或GUID作为键,字符串作为值.
但正如其他人所指出的,您还可以创建一个Sector基本上由ID和名称组成的类,并将该类的实例放入列表中.
您在使用Hashtable/Dictionary时获得的是您可以通过ID快速查找.当您在列表中搜索特定ID时,您必须遍历列表(好吧,如果它已经排序,您可以使用二进制搜索),而哈希表通常只需要一次查找.
| 归档时间: |
|
| 查看次数: |
1098 次 |
| 最近记录: |