处理我的二维数组

发布于 2024-11-08 21:14:49 字数 414 浏览 0 评论 0原文

我只是想问什么是解决二维数组(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

流绪微梦 2024-11-15 21:14:49
public class VoteManager
{
    public Dictionary<string, int> Votes { get; private set; }
    public VoteManager
    {
        Votes = new Dctionary<string, int>();
    }
    public void AddVotes(string name, int voteCount)
    {
        int oldCount;
        if (!Votes.TryGetValue(name, out oldCount))
            oldCount = 0;
        Votes[name] = oldCount + voteCount;
    }
public class VoteManager
{
    public Dictionary<string, int> Votes { get; private set; }
    public VoteManager
    {
        Votes = new Dctionary<string, int>();
    }
    public void AddVotes(string name, int voteCount)
    {
        int oldCount;
        if (!Votes.TryGetValue(name, out oldCount))
            oldCount = 0;
        Votes[name] = oldCount + voteCount;
    }
秋日私语 2024-11-15 21:14:49

您应该使用关联数组。对于 C# 来说,这样的集合就是Dictionary

var votes = new Dictionary<string, int>();
votes["John"] = 10;
votes["Bob"] = 20;
votes["John"] = 15; // replaces earlier setting

如果您想添加到现有投票中,您需要检查是否存在现有值:

private Dictionary<string, int> votesByPeep; // initialized in constructor

private void AddVotes(string peep, int votes)
{
    if (this.votesByPeep.ContainsKey(peep)
    {
        this.votesByPeep[peep] += votes;
    }
    else
    {
        this.votesByPeep[peep] = votes;
    }
}

You should use an Associative Array. In the case of C#, such a collection is the Dictionary.

var votes = new Dictionary<string, int>();
votes["John"] = 10;
votes["Bob"] = 20;
votes["John"] = 15; // replaces earlier setting

If you want to add to the exisiting vote, you will need to check if there is an existing value:

private Dictionary<string, int> votesByPeep; // initialized in constructor

private void AddVotes(string peep, int votes)
{
    if (this.votesByPeep.ContainsKey(peep)
    {
        this.votesByPeep[peep] += votes;
    }
    else
    {
        this.votesByPeep[peep] = votes;
    }
}
德意的啸 2024-11-15 21:14:49

为什么不定义一个具有两个属性(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.

孤芳又自赏 2024-11-15 21:14:49

听起来更好的解决方案是使用 Dictionary。字典/哈希表非常适合将值(投票数)与给定键(用户名)配对的场景。它使更新和查找场景变得非常简单

class Container {
  private Dictionary<string, int> m_voteMap = new Dictionary<string, int>();

  public void SetVote(string user, int votes) {
    m_voteMap[user] = votes;
  }

  public int GetVotes(string user) {
    int votes;
    if (!m_voteMap.TryGetValue(user, out votes)) {
      votes = 0;
    }
    return votes;
  }
}

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

class Container {
  private Dictionary<string, int> m_voteMap = new Dictionary<string, int>();

  public void SetVote(string user, int votes) {
    m_voteMap[user] = votes;
  }

  public int GetVotes(string user) {
    int votes;
    if (!m_voteMap.TryGetValue(user, out votes)) {
      votes = 0;
    }
    return votes;
  }
}
白云悠悠 2024-11-15 21:14:49

您可以使用从字符串(名称)到整数(投票)的字典,这将为您提供 {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

扶醉桌前 2024-11-15 21:14:49

创建一个名为 CandidateVotes 的类,并将其存储在 List 集合中。

public class CandidateVotes
{
    public string Name {get; set;}
    public int Votes {get; set;}
}

Create a class called CandidateVotes, and store that in a List<CandidateVotes> collection.

public class CandidateVotes
{
    public string Name {get; set;}
    public int Votes {get; set;}
}
刘备忘录 2024-11-15 21:14:49
Dictionary<string, int> is your friend
Dictionary<string, int> is your friend
千里故人稀 2024-11-15 21:14:49

这听起来像是 Dictionary 的一个很好的候选者。在本例中,Dictionary,键是候选人,值是投票数。

// Create dictionary as:
Dictionary<string, int> votes = new Dictionary<string, int>();

然后,您可以制定一些如下例程:

void AddVotes(string candidate, int numberOfVotes)
{
    if (this.votes.Contains(candidate))
    {
         // Update the "10 to 12" in your scenario
         int current = this.votes[candidate];
         current += numberOfVotes;
         this.votes[candidate] = current;
    }
    else
         this.votes[candidate] = numberOfVotes; // First time a candidate is used...
}

当您想要列出每个候选人的选票时,您可以执行以下操作:

foreach(var pair in this.votes)
{
    Console.WriteLine("Candidate {0} has {1} votes.", pair.Key, pair.Value);
}

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.

// Create dictionary as:
Dictionary<string, int> votes = new Dictionary<string, int>();

You could then make some routines like the following:

void AddVotes(string candidate, int numberOfVotes)
{
    if (this.votes.Contains(candidate))
    {
         // Update the "10 to 12" in your scenario
         int current = this.votes[candidate];
         current += numberOfVotes;
         this.votes[candidate] = current;
    }
    else
         this.votes[candidate] = numberOfVotes; // First time a candidate is used...
}

When you want to list out the votes per candidate, you can do something like:

foreach(var pair in this.votes)
{
    Console.WriteLine("Candidate {0} has {1} votes.", pair.Key, pair.Value);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文