

List or Dictionary or something else?
source link: https://www.codesd.com/item/list-or-dictionary-or-something-else.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.
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.
The xPos is allways the same:
There is a "chart" where I place some blocks in a row with different yPos.
1. Block: xPos = 0, yPos = random
2. Block: xPos = 1, yPos = random
...
n. Block: xPos = 80, yPos = random
n+1. Block: xPos = 0, yPos = 20 + random
I have three iterations, for each 80 Blocks are placed.

SORRY for my bad english :|
I hope you could understand.
Or you can use List
of Tuple
to store list of int
-int
pairs without creating new class and without worrying about duplicate values :
.....
List<Tuple<int, int>> blocks = new List<Tuple<int, int>>();
blocks.Add(Tuple.Create(0, random));
blocks.Add(Tuple.Create(1, random));
.....
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK