2015-11-04 156 views
1

假設我有多維對象數組,表示例如僱員信息字符串圖案在多維字符串數組搜索[,]

String[,] Employee= new object[,] { 
      {"john","MBA#MBA#BE#MBA#BE"}, 
      {"jan","MS#MSC#MBA"}, 

}; 

可視

john     MBA#MBA#BE#MBA#BE  
jan     MS#MSC#MBA 

我想刪除的複製數據(其中數據除以#)

john     MBA#BE 
jan     MS#MSC#MBA 
+1

爲什麼不創建一個適當的類而不是使用數組? – juharr

+0

你最好使用這個字典。 –

回答

1

對於這種情況,最好使用字典:

Dictionary<string, string> employee = new Dictionary<string, string>() 
{ 
    {"john", "MBA#MBA#BE#MBA#BE"}, 
    {"jan", "MS#MSC#MBA"} 
} 

Dictionary<string, string> result = new Dictionary<string, string>(); 

foreach (var kvp in employee) 
{ 
    result[kvp.Key] = string.Join("#", kvp.Value.Split('#').Distinct()); 
} 

result字典將包含結果:

// Prints "MS#MSC#MBA" 
Console.WriteLine(result["jan"]); 

// Prints "MBA#BE" 
Console.WriteLine(result["john"]); 
0

個人而言,我想創建一個類來保存這些信息

public class Employee 
{ 
    public Employee(string name) 
    { 
     Name = name; 
     Degress = new HashSet<string>(); 
    } 

    public string Name { get; private set; } 
    public HashSet<string> Degrees { get; private set; } 
} 

然後,你可以這樣來填充他們的列表

string[,] employeeArray = { { "john", "MBA#MBA#BE#MBA#BE" }, { "jan", "MS#MSC#MBA" } }; 

List<Employee> employees = new List<Employee>(); 

for (int i = 0; i < employeeArray.GetLength(0); i++) 
{ 
    Employee employee = new Employee(employeeArray[i,0]); 
    foreach (var degree in employeeArray[i, 1].Split('#')) 
    { 
     employee.Degrees.Add(degree); 
    } 

    employees.Add(employee); 
} 

foreach (var e in employees) 
{ 
    Console.WriteLine(e.Name + " " + string.Join(" ", e.Degress)); 
} 

它打印

約翰MBA BE

揚MS MSC MBA

因爲DegreesHashSet它不會允許重複,當你試圖複製傳遞給Add它會只需返回false