这是我的通用清单:
公共类TagType {公共字符串FieldTag;公共int位置;}
List<TagType<dynamic>> TagList = new List<TagType<dynamic>>();
TagList.Add(new TagType<dynamic>() { FieldTag = "ID", Position = posIdStr });
TagList.Add(new TagType<dynamic>() { FieldTag = "PT", Position = posPtStr });
TagList.Add(new TagType<dynamic>() { FieldTag = "ID", Position = posIdStr });
TagList.Add(new TagType<dynamic>() { FieldTag = "EC", Position = posECStr });我正试图为后面的FieldTag (例如: PT)获取位置值。我该怎么做?
发布于 2015-03-14 09:46:47
您找到PT的索引并添加1吗?(但请记住要检查索引+1<列表的长度)
// Find the index of PT
int ix = TagList.FindIndex(x => x.FieldTag == "PT");
// index found
if (ix != -1)
{
// Check that index + 1 < the length of the List
if (ix + 1 < TagList.Count)
{
var position = TagList[ix + 1]; // Add 1
}
}发布于 2015-03-14 10:05:51
不幸的是,每次执行搜索时,您都必须遍历列表,找到要查找的字段标记,然后转到下一个元素并获取位置值。e..:用于查找解决方案的O(n):
private static object SearchPosition(List<TagType<object>> tagList, string fieldTag)
{
var i = tagList.FindIndex(x => x.FieldTag == "PT");
if (i >= 0 && i < tagList.Count)
{
return tagList[i + 1].Position;
}
}和测试:
[Test]
public void FieldTagTest()
{
var res = SearchPosition(_tagList, "PT");
res.ToString().Should().Be("ID2");
}如果列表不经常更改,则应该生成一个Dictionary<string,int>,其中FieldTag作为Key,列表索引位置为value。当然,每次修改列表时,都需要再次构建此索引。
O(1)解决方案是:
private static object SearchPositionUsingIndex(List<TagType<object>> tagList, string fieldTag)
{
// You would save this index, and build it only once,
// or rebuild it whenver something changes.
// you could implement custom index modifications.
var index = BuildIndex(tagList);
int i;
if (!index.TryGetValue(fieldTag, out i)) return null;
if (i + 1 >= tagList.Count) return null;
return tagList[i + 1].Position;
}
private static Dictionary<string, int> BuildIndex(List<TagType<object>> tagList)
{
var index = new Dictionary<string, int>();
for (int i = 0; i < tagList.Count; i++)
{
var tag = tagList[i];
if (!index.ContainsKey(tag.FieldTag)) index.Add(tag.FieldTag, i);
}
return index;
}和测试:
[Test]
public void FieldTagTestUsingIndex()
{
var res = SearchPositionUsingIndex(_tagList, "PT");
res.ToString().Should().Be("ID2");
}或者您可以使用1行LINQ方法,也就是O(n)
[Test]
public void FieldTagTestLinq()
{
var res = SearchUsingLinq();
res.ToString().Should().Be("ID2");
}
private object SearchUsingLinq()
{
var p = _tagList.SkipWhile(x => x.FieldTag != "PT").Skip(1).FirstOrDefault();
return p != null ? p.Position : null;
}TestSetup
public class SO29047477
{
private List<TagType<object>> _tagList;
[SetUp]
public void TestSetup()
{
_tagList = new List<TagType<dynamic>>();
_tagList.Add(new TagType<dynamic>() { FieldTag = "ID", Position = "ID1"});
_tagList.Add(new TagType<dynamic>() { FieldTag = "PT", Position = "PT1" });
_tagList.Add(new TagType<dynamic>() { FieldTag = "ID", Position = "ID2" });
_tagList.Add(new TagType<dynamic>() { FieldTag = "EC", Position = "EC1" });
}
}发布于 2015-03-14 09:55:43
如果希望在每个项目之后使用LINQ PT获得下一个元素的Position,那么可以用LINQ在一两行中解决它:
var resultTag = TagList.SkipWhile(x => x.FieldTag != "PT").Skip(1).FirstOrDefault();
var resultPosition = resultTag == null ? 0 : resultTag.Position;附加信息:
如果您想将其转换为int,那么只需显式地转换它即可。
var resultTag = TagList.SkipWhile(x => x.FieldTag != "PT").Skip(1).FirstOrDefault();
int resultPosition = resultTag == null ? 0 : (int)resultTag.Position;https://stackoverflow.com/questions/29047477
复制相似问题