我有一個這樣的整數串列串列:
public List<List<int>> Paths = new List<List<int>>
{
new List<int>{0,1 },
new List<int>{1,2 },
new List<int>{1,3 },
new List<int>{2,3 },
new List<int>{2,4 },
new List<int>{3,4 },
new List<int>{4,5 },
};
我有一條路徑,這只是一個 int 串列:
List<int> path = new List<int>{4,5};
如何檢查是否Paths包含path?我已經嘗試過if(Paths.Contains(path)),它總是產生false,即使我知道我在那里有一個串列{4, 5}。
我正在開發一個統一專案,我已經閱讀了一些可以使用 linq 完成的魔法,所以我在標題主題中為未來的谷歌用戶標記了它。;)
非常感謝你的幫助!
編輯:我想出了這個臨時解決方案,但我覺得它可以做得更優雅:
public bool CheckIfPathsHaveConnection(List<int> connection)
{
bool hasElement = false;
foreach(List<int> path in Paths)
{
if(path[0] == connection[0] && path[1] == connection[1])
{
hasElement = true;
break;
}
else
{
hasElement = false;
}
}
return hasElement;
}
EDIT2:謝謝大家的回復,我真的很感激你的努力。每個答案都很有幫助,值得一個解決方案,不幸的是我只能選擇一個作為解決方案,所以我選擇了解釋最多的一個。
uj5u.com熱心網友回復:
if(Paths.Contains(path))
在您的情況下,總是回傳,false因為List<T>它是一個類 => 參考型別,并且您檢查了參考相等性(它必須是串列的完全相同的實體),但是您創建了new一個。
對于實際串列和更多一般情況,您可以使用
using System.Linq;
...
// If you use array or list doesn't matter for Linq / IEnumerable
var index = paths.FindIndex(p => p.SequenceEqual(new []{1, 2});
if(index >= 0)
{
...
}
回傳第一個遇到的具有相同順序的相同專案的串列的索引,或者-1如果沒有找到。
看
SequenceEqualFindIndex
但是,如果無論如何總是只有并且恰好兩個專案,那么我根本不會使用串列,而是使用IEquatableand 特別是GetHashCode(用于任何基于哈希的集合Dictionary,HashSet例如等),例如
public class Path : IEquatable<Path>
{
public int Start;
public int End;
public Path(int start, int end)
{
Start = start;
End = end;
}
public override int GetHashCode()
{
return Start.GetHashCode() ^ End.GetHashCode();
}
public bool Equals(Path path)
{
return path.Start == Start && path.End == End;
}
public override bool Equals(object obj)
{
if (obj == null || ! (obj is Path path))
return false;
else
return Equals(path);
}
public static bool operator == (Path a, Path b)
{
if (((object)a) == null || ((object)b) == null)
return object.Equals(a, b);
return a.Equals(b);
}
public static bool operator != (Path a, Path b)
{
if (((object)a) == null || ((object)b) == null)
return ! object.Equals(a, b);
return ! (a.Equals(b));
}
}
然后你可以簡單地擁有一個
public List<Path> Paths = new List<Path>
{
new Path (0,1),
new Path (1,2),
new Path (1,3),
....
}
并在完全不使用 Linq 的情況下獲取索引
// Since you now properly implemented Equals this now compares
// the values rather than the reference
var index = paths.IndexOf(new Path(1, 2));
if(index >= 0)
{
...
}
uj5u.com熱心網友回復:
嘗試if (Paths.Any(x => x[0] == path[0] && x[1] == path[1]))
uj5u.com熱心網友回復:
如果你想要一個 linq 解決方案,你可以使用Any()和的組合All():
var containsPath = Paths.Any(p => p // any sub-list in 'Paths'
.All(q => // all elements of sub-list
path.Contains(q))); // are contained in 'path'
uj5u.com熱心網友回復:
如果你想知道現有路徑的索引,試試這個
var index = IfExist(paths,path);
var exist = index >= 0;
public int IfExist(List<List<int>> paths, List<int> path)
{
for (int i = 0; i < paths.Count; i )
if (paths[i][0] == path[0] && (paths[i][1] == path[1])) return i;
return -1;
}
uj5u.com熱心網友回復:
您想要的是測驗并查看是否有任何路徑Paths等于path:
var ans = Paths.Any(p => p.SequenceEqual(path));
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/425623.html
下一篇:創建字串的條件字典以替換名稱
