从名称数组中获取下一个可用 ID
Get next available ID from array of names
我有 3 个字符串,每个都包含在一个数组中。
string[] folderArray = {"1 - Name", "4 - Another name", "3 - Another name"}
我需要获取下一个可用的 ID(文件夹编号)。
例如,所需的 ID 为 2,因为它会发现数组中缺少“2”ID。
可以在 Finding first available ID from an array
找到此问题的 PHP 个副本
试试这个代码
string[] folderArray = { "1 - Name", "4 - Another name", "3 - Another name" };
var listId = new List<int>();
foreach (var item in folderArray)
{
var tempId = 0;
if (int.TryParse(item.Split('-')[0].Trim(), out tempId))
listId.Add(tempId);
}
listId.Sort();
for (var i=1 ;i<listId.Count ;i++)
{
if(listId[i]-listId[i-1]>1)
{
Console.WriteLine(listId[i-1]+1);
break;
}
}
我不会这样做,但如果您想要与您链接的 PHP 解决方案中的相同:
var missing = Enumerable.Range(1, folderArray.Length).Except(folderArray.Select(f => int.Parse(f.Split('-').First()))).First();
注意:假设 folderArray 中的项目始终采用
形式
- "[数字] - [一些字符串]"
没有空字符串,没有空值等
思考以下几点:
- 不要使用字符串数组,使用带有正确
IComparer<int>
的排序集合(例如 SortedList<int, String>
)(尝试 Comparer<int>.Default
)
- 然后依次迭代,找到第一个gap的gap的意思是:
list.ContainsKey(n) == true && list.ContainsKey(n + 1) == false
——那么下一个ID就是n + 1
- 为了提高性能,您可以记住您给呼叫者的最后一个 ID(或者最后一个被删除的文件夹,如果支持的话),然后从那里开始搜索下一个 ID 请求
我想 post 将此作为评论,但它不适用于格式 :-/。
如果你使用 SortedList class,你会解决这个问题...看看这段代码
SortedList<int, string> lista = new SortedList<int, string>();
lista.Add(4, "Name");
lista.Add(1, "Name");
lista.Add(3, "Name");
lista.Add(7, "Name");
int nextID = 1;
foreach (var item in lista.Keys)
{
if (nextID != item) break;
else nextID++;
}
Console.WriteLine(nextID);
SortedList class 接收一个 TKey 参数和一个 TValue 参数。她通过 TKey 参数对元素进行排序。您只需在列表中添加元素,她会为您完成这些工作……然后,搜索列表中不存在的下一个 Id。 lista.Keys returns 添加了所有键的 IEnumerable,按降序排列...
我有 3 个字符串,每个都包含在一个数组中。
string[] folderArray = {"1 - Name", "4 - Another name", "3 - Another name"}
我需要获取下一个可用的 ID(文件夹编号)。
例如,所需的 ID 为 2,因为它会发现数组中缺少“2”ID。
可以在 Finding first available ID from an array
找到此问题的 PHP 个副本试试这个代码
string[] folderArray = { "1 - Name", "4 - Another name", "3 - Another name" };
var listId = new List<int>();
foreach (var item in folderArray)
{
var tempId = 0;
if (int.TryParse(item.Split('-')[0].Trim(), out tempId))
listId.Add(tempId);
}
listId.Sort();
for (var i=1 ;i<listId.Count ;i++)
{
if(listId[i]-listId[i-1]>1)
{
Console.WriteLine(listId[i-1]+1);
break;
}
}
我不会这样做,但如果您想要与您链接的 PHP 解决方案中的相同:
var missing = Enumerable.Range(1, folderArray.Length).Except(folderArray.Select(f => int.Parse(f.Split('-').First()))).First();
注意:假设 folderArray 中的项目始终采用
形式- "[数字] - [一些字符串]"
没有空字符串,没有空值等
思考以下几点:
- 不要使用字符串数组,使用带有正确
IComparer<int>
的排序集合(例如SortedList<int, String>
)(尝试Comparer<int>.Default
) - 然后依次迭代,找到第一个gap的gap的意思是:
list.ContainsKey(n) == true && list.ContainsKey(n + 1) == false
——那么下一个ID就是n + 1
- 为了提高性能,您可以记住您给呼叫者的最后一个 ID(或者最后一个被删除的文件夹,如果支持的话),然后从那里开始搜索下一个 ID 请求
我想 post 将此作为评论,但它不适用于格式 :-/。
如果你使用 SortedList class,你会解决这个问题...看看这段代码
SortedList<int, string> lista = new SortedList<int, string>();
lista.Add(4, "Name");
lista.Add(1, "Name");
lista.Add(3, "Name");
lista.Add(7, "Name");
int nextID = 1;
foreach (var item in lista.Keys)
{
if (nextID != item) break;
else nextID++;
}
Console.WriteLine(nextID);
SortedList class 接收一个 TKey 参数和一个 TValue 参数。她通过 TKey 参数对元素进行排序。您只需在列表中添加元素,她会为您完成这些工作……然后,搜索列表中不存在的下一个 Id。 lista.Keys returns 添加了所有键的 IEnumerable,按降序排列...