在 C# 中,我想要一个将 (x,y) 坐标映射到 (x,y) 的数据结构。我怎样才能做这样的事情?
我不想使用类似 的公式将 x,y 坐标转换为单个值y*w+x。有没有办法可以拥有dictionary<key,key,(value,value)>.
如果我将键作为 Tuple,那么它是一个对象,并且 Tuple(1,1) 不等于 Tuple(1,1)。所以我认为我无法找到这个意义上的钥匙。
我最近一直在玩一些字节数组(处理灰度图像).一个字节的值可以是0-255.我正在修改字节,并遇到了我分配给字节的值超出字节范围的情况.它正在为我正在玩的图像做出意想不到的事情.
我写了一个测试,并了解到该字节仍然存在.例:
private static int SetByte(int y)
{
return y;
}
.....
byte x = (byte) SetByte(-4);
Console.WriteLine(x);
//output is 252
Run Code Online (Sandbox Code Playgroud)
有一个结转!当我们走另一条路时,就会发生这种情况.
byte x = (byte) SetByte(259);
Console.WriteLine(x);
//output is 3
Run Code Online (Sandbox Code Playgroud)
我希望它在第一种情况下将其设置为255,在第二种情况下将其设置为0.这个结转的目的是什么?是不是因为我正在投射这个整数赋值?什么时候在现实世界中有用?
我有两个字符串 StringA、StringB。我想生成一个唯一的字符串来表示这一对。
IE
f(x, y) 对于每个 x、y 和 f(x, y) = f(y, x) 应该是唯一的,其中 x、y 是字符串。
有任何想法吗?
我想使用自定义泛型类作为字典中的键.我应该如何重写Equals和GetHashCode?
例如,
public class SomeKey<T,V>
{
public T Value1 { get; set; }
public V Value2 { get; set; }
public SomeKey(T val1, V val2)
{
this.Value1 = val1;
this.Value2 = val2;
}
public override bool Equals(SomeKey<T,V> otherKey)
{
//whats the best option here?
}
public override int GetHashCode()
{
//whats the best option here?
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我在这里读到了一些与GetHashCode正确实施相关的问题.我没找到的是什么时候应该实现这个方法.
在我的具体情况下,我构建了一个简单的不可变结构:
public struct MyStruct
{
private readonly Guid m_X;
private readonly string m_Y;
private readonly string m_Z;
public Guid string X
{
get { return m_X; }
}
public string Y
{
get { return m_Y; }
}
public string Z
{
get { return m_Z; }
}
public MyStruct(Guid x, string y, string z)
{
if (x == Guid.Empty) throw new ArgumentException("x cannot be equals to Guid.Empty", "x");
if (string.IsNullOrEmpty(y)) throw new ArgumentException("y cannot …Run Code Online (Sandbox Code Playgroud) 我需要你的帮助.我试图从对象列表中获取不同的值.我的班级看起来像这样:
class Chromosome
{
public bool[][] body { get; set; }
public double fitness { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我有List<Chromosome> population.现在我需要的是一种方式,我如何获得新的清单:List<Chromosome> newGeneration.这个新列表将只包含原始列表中的唯一染色体 - 种群.
我有一个包含三列的SQL Server表:
表格1
col1 int
col2 int
col3 string
Run Code Online (Sandbox Code Playgroud)
我为所有三列定义了一个唯一约束 (col1, col2, col3)
现在,我有一个.csv文件,我想在此表中添加记录,*.csv文件可以有重复记录.
我在上面的场景中搜索了各种避免重复的选项.以下是适合我的三个选项.请看一下并提出一些关于每种方法的优点/缺点的想法,以便我可以选择最好的方法.
选项1 :
首先避免重复,即从csv文件向列表添加对象时.我用过HashSet<T>了这个并覆盖了类型T下面的方法:
public override int GetHashCode()
{
return col1.GetHashCode() + col2.GetHashCode() + col3.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as T;
if (other == null)
{
return false;
}
return col1 == other.col1
&& col2 == other.col2
&& col3 == other.col3;
}
Run Code Online (Sandbox Code Playgroud)
选项#2
有List<T>代替HashSet<T>.
添加所有对象后删除重复项 List<T>
List<T> distinctObjects = allObjects
.GroupBy(x => new …Run Code Online (Sandbox Code Playgroud) 我正在尝试在列表之间创建一个检查,但我没有运气:-/
我有一个包含 100 个字段的游戏板,并进行此循环以仅将空字段添加到新列表中:
for(int i = 0; i < thisGame.boardFields.Count; i++)
{
if (thisGame.boardFields.Count != 0 && thisGame.boardFields [i] != null)
{
BoardField thisField = thisGame.boardFields [i];
if (thisField.owner == "0" && thisField.number != 13)
{
Tile tTile = new Tile();
tTile.color = thisField.color;
tTile.number = thisField.number.ToString();
tempBoard.Add (tTile);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我遍历玩家 5 块瓷砖,看看玩家是否有一块不可玩的瓷砖,例如,具有相同对象的空字段不可用,如下所示:
for (var i = 0; i < thisGame.playerTiles.Count; i++)
{
Tile tempTile = new Tile();
tempTile.color = thisGame.playerTiles[i].color;
tempTile.number = thisGame.playerTiles[i].number.ToString();
if (!tempBoard.Contains (tempTile)) …Run Code Online (Sandbox Code Playgroud) 请参阅下面的代码.
static void Main(string[] args)
{
// Create Dictionary
var dict = new Dictionary<TestClass, ValueClass>();
// Add data to dictionary
CreateSomeData(dict);
// Create a List
var list = new List<TestClass>();
foreach(var kv in dict) {
// Swap property values for each Key
// For example Key with property value 1 will become 6
// and 6 will become 1
kv.Key.MyProperty = 6 - kv.Key.MyProperty + 1;
// Add the Key to the List
list.Add(kv.Key);
}
// Try to print dictionary …Run Code Online (Sandbox Code Playgroud) 背景
在 PowerShell 中构建哈希表以通过特定属性快速访问对象是很常见的,例如基于以下内容建立索引LastName:
$List = ConvertFrom-Csv @'
Id, LastName, FirstName, Country
1, Aerts, Ronald, Belgium
2, Berg, Ashly, Germany
3, Cook, James, England
4, Duval, Frank, France
5, Lyberg, Ash, England
6, Fischer, Adam, Germany
'@
$Index = @{}
$List |ForEach-Object { $Index[$_.LastName] = $_ }
$Index.Cook
Id LastName FirstName Country
-- -------- --------- -------
3 Cook James England
Run Code Online (Sandbox Code Playgroud)
在某些情况下,需要在两个(甚至更多)属性上构建索引,例如 theFirstName和 the LastName。为此,您可以创建一个多维密钥,例如:
$Index = @{}
$List |ForEach-Object {
$Index[$_.FirstName] = @{} …Run Code Online (Sandbox Code Playgroud) c# ×9
dictionary ×2
.net ×1
contains ×1
hash ×1
hashset ×1
hashtable ×1
indexing ×1
linq ×1
list ×1
powershell ×1
sql-server ×1
string ×1
unique ×1