这是我的通用列表:
公共类TagType { 公共字符串FieldTag; public int Position; }
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)。 我该怎么做?
答案 0 :(得分:2)
您找到PT
的索引并添加1? (但记得检查索引+ 1&lt; List的长度)
// 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
}
}
答案 1 :(得分:1)
不幸的是,每次进行搜索时,您都必须遍历列表,找到您要查找的字段标记,然后转到下一个元素并获取位置值。 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" });
}
}
答案 2 :(得分:0)
如果你想在Position
PT的每个项目之后获得下一个元素FieldTag
,那么你可以使用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;
答案 3 :(得分:0)
public Position? GetNextPosition(string FieldTagVal)
{
bool returnNext = false;
foreach(TagType t in TagList)
{
if (returnNext) return t.Position;
if (t.FieldTag == FieldTagVal) returnNext = true;
}
return null;
}