我有一个名单和电话号码列表如下: var phonelist = Liststring[] { new string[] {"Bill", "1234", "12345", "12314" }, new string[] {"Bob", "", "12345", "12314" }, new string[] {"Chris", "", "", "12314" }, new string[] {"Dave", "
var phonelist = List<string[]> { new string[] {"Bill", "1234", "12345", "12314" }, new string[] {"Bob", "", "12345", "12314" }, new string[] {"Chris", "", "", "12314" }, new string[] {"Dave", "1234", "", "12314" }, new string[] {"Andy", "1234", "12345", "" }, }
处理此列表的最有效/优雅的方法是什么,以便从右边填充“空”数字?
注意,数组应保持相同的长度,如下所示:
var phonelist = List<string[]> { new string[] {"Bill", "1234", "12345", "12314" }, new string[] {"Bob", "12345", "12314", "" }, new string[] {"Chris", "12314", "", "" }, new string[] {"Dave", "1234", "12314", "" }, new string[] {"Andy", "1234", "12345", "" }, }对于每个阵列单元格,检查它是否为空并将其与单元格1交换,如果它仍为空则将其与单元格2交换..当单元格变为空时,对单元格2执行相同的操作…
int j; foreach (string[] strs in phoneList) { for (int i = 0; i < strs.Length; i++) { j = 1; while (string.IsNullOrEmpty(strs[i]) && j < strs.Length - i) { if (!string.IsNullOrEmpty(strs[i + j])) // to not swap 2 empty strings { strs[i] = strs[i + j]; strs[i + j] = ""; } j++; } } }