处理我的二维数组
我只是想问什么是解决二维数组(2 列)的最佳方法,该数组将存储:CandidateName 及其各自的 VoteCount。
我真正想做的是,接受用户的输入:VOTE John 10,其中 John 是候选人的名字,10 是他想给他的选票。所以我需要将 {John, 10} 存储到我的数组中。但是,在此之后,我的程序将再次要求用户投票,因此如果我输入 VOTE Doe 15,则条目 {Doe, 15} 将被添加到数组中。如果用户输入 VOTE John 2,我的数组需要更新,因此新值将是 {John, 12}。
目前我使用两个数组列表:CandidateName 和 VoteCount,我只依靠它们的索引进行配对。然而,这并不是很可靠,所以我正在尝试寻找另一种方法来解决这个问题。然而,我并不是多维数组的忠实粉丝。
有人可以指出我如何实现这一目标的好方法吗?
I just wanna ask what best way to work around a Two-Dimensional Array (2 Columns) which would store: CandidateName and their respective VoteCount.
What I want exactly to do is to, accept an input from the user say: VOTE John 10 wherein John is the name of the candidate and 10 is the votes that he wanna give him. So I need to store {John, 10} into my array. However, after this my program would once again ask the user for votes so if I enter VOTE Doe 15, the entry {Doe, 15} would then be added to the array. If the user enters VOTE John 2, my array needs to be updated and thus the new value would be {John, 12}.
Currently I use two arraylists: CandidateName and VoteCount and I just rely on their index for pairing. However, this isn't really reliable so I'm trying to find another way on how to solve this. However, I'm not really a big fan of multi-dimensional arrays.
Can someone please point me out to a good way on how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您应该使用关联数组。对于 C# 来说,这样的集合就是
Dictionary
。如果您想添加到现有投票中,您需要检查是否存在现有值:
You should use an Associative Array. In the case of C#, such a collection is the
Dictionary
.If you want to add to the exisiting vote, you will need to check if there is an existing value:
为什么不定义一个具有两个属性(Name 和 VoteCount)的结构/类。那么你只需要一个数组。
编辑:
我建议这样做是因为您可能想向候选添加其他操作或属性。如果您需要的只是这两个值之间的关联,那么字典是正确的解决方案。
Why don't you define a struct/class with two properties, Name and VoteCount. Then you only need one array.
EDIT:
I suggested this because there may be additional operations or properties you want to add to Candidates. If all you need is an association between these two values, a dictionary is the correct solution.
听起来更好的解决方案是使用
Dictionary
。字典/哈希表非常适合将值(投票数)与给定键(用户名)配对的场景。它使更新和查找场景变得非常简单It sounds like a much better solution here is to use a
Dictionary<TKey, TValue>
. A dictionary / hashtable is ideal for a scenario where you're pairing a value (vote count) with a given key (user name). It makes for very easy update and lookup scenarios您可以使用从字符串(名称)到整数(投票)的字典,这将为您提供 {name, votes} 对和一个很好的快速查找
You can use a dictionary from strings (names) to int (votes), this will give you the {name, votes} pair and a nice quick lookup
创建一个名为
CandidateVotes
的类,并将其存储在List
集合中。Create a class called
CandidateVotes
, and store that in aList<CandidateVotes>
collection.这听起来像是
Dictionary
的一个很好的候选者。在本例中,Dictionary
,键是候选人,值是投票数。然后,您可以制定一些如下例程:
当您想要列出每个候选人的选票时,您可以执行以下操作:
This sounds like a good candidate for a
Dictionary<T,U>
. In this case,Dictionary<string,int>
, with the key being the candidate, and the value being the vote count.You could then make some routines like the following:
When you want to list out the votes per candidate, you can do something like: