简体   繁体   English

列表或字典还是其他?

[英]List or Dictionary or something else?


I want to save some coordinates in a dictionary, BUT the xPos should be twice or more in the dictionary. 我想在字典中保存一些坐标,但是xPos在字典中应该是两倍或更多。

The problem is that the following exception appears: 问题是出现以下异常:
ArgumentException: An element with the same key already exists in the dictionary.

How can I solve the problem ? 我该如何解决这个问题?

I allready thought that I can use a List or an Array, but I want a Key and a Value. 我已经以为可以使用列表或数组,但我想要一个键和一个值。

After I saved the coordinates in a Dict (or something else) I want to check whether a new coordinate is a certain distance of the existing ones. 将坐标保存在Dict(或其他)中后,我想检查新坐标是否与现有坐标相距一定距离。

The xPos is allways the same: xPos始终相同:
There is a "chart" where I place some blocks in a row with different yPos. 有一个“图表”,我在其中以不同的yPos放置了一些块。
1. Block: xPos = 0, yPos = random 1.区块:xPos = 0,yPos =随机
2. Block: xPos = 1, yPos = random 2. Block:xPos = 1,yPos =随机
... ...
n. Block: xPos = 80, yPos = random 区块:xPos = 80,yPos =随机
n+1. n + 1。 Block: xPos = 0, yPos = 20 + random 阻止:xPos = 0,yPos = 20 +随机

I have three iterations, for each 80 Blocks are placed. 我有三个迭代,每放置80个块。

在此处输入图片说明

SORRY for my bad english :| 对不起,我的英语不好:|
I hope you could understand. 我希望你能理解。

You can create a class (or a struct) to keep and use your coordinates, instead of a dictionary. 您可以创建一个类(或结构)来保留和使用您的坐标,而不是字典。 The class can have Key and Value properties and also additional fields if needed. 该类可以具有键和值属性,如果需要,还可以具有其他字段。

You should save the values like List, where Position contains: 您应该保存诸如List之类的值,其中Position包含:

public int X { get; set; }
public int Y { get; set; }

Or you can use some another class/struct from C# or some 3rd libraries (2d vector, point, etc) depends on where you want to use it. 或者,您可以使用C#中的其他类/结构或某些3rd库(2d矢量,点等),具体取决于您希望在何处使用它。 :) :)

Or you can use List of Tuple to store list of int - int pairs without creating new class and without worrying about duplicate values : 或者你可以使用ListTuple存储的名单int - int对不创建新类,而不必担心重复的值:

.....
List<Tuple<int, int>> blocks = new List<Tuple<int, int>>();
blocks.Add(Tuple.Create(0, random));
blocks.Add(Tuple.Create(1, random));
.....

Since you're storing points in a 2-d surface and want to do nearest-point detection, I'd recommend using a Binary Space Partitioning (BSP) tree , such as a QuadTree. 由于您要将点存储在二维曲面中,并且想要进行最近点检测,因此建议您使用Binary Space Partitioning(BSP)树 ,例如QuadTree。 Here's a link to a quadtree implementation in C#. 这是C#中四叉树实现的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用List &lt;&gt;,Array还是其他? - Use List<>, Array or something else? 传递到字典中的模型项的类型为“ System.Collections.Generic.List`1 [System.String]”,但要求类型为“其他” - The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String]' but requires type “Something else” 传递到字典中的模型项的类型为“ something”。传递到字典中的模型项的类型为“ something else” - The model item passed into the dictionary is of type “ something” The model item passed into the dictionary is of type “something else” C#返回字典查找的结果,或者如果NULL返回其他内容 - C# Return Results of Dictionary Lookup or if NULL return something else 如何将一组&lt;=,&gt; = if ... else语句重构为字典或类似的东西 - how to refactor a set of <= , >= if…else statements into a dictionary or something like that 清单C#问题的清单,或其他与此编码有关的指针 - List of List C# question or something else any pointers in coding this 是清单 <T> 表达式树中的常量,表达式参数或其他内容? - Is List<T> a constant, an expression parameter, or something else in Expression Tree? 在做其他事情时如何使用Linq构造列表? - how to construct a list using Linq, while doing something else? 字典 <something, List<> &gt;展平(取消分组)到列表中的某物和元素的列表-C# - Dictionary<something, List<>> flatten (ungroup) to List of something and elements from List - C# 阅读此 XML 到字典(Linq 或其他东西?)的最快/最有效的方法是什么? - What is the fastest/most efficient way to read this XML to Dictionary (Linq or something else?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM