2011-05-20 15 views
0

2次元配列(2列)を保存する最良の方法は、CandidateNameとそれぞれのVoteCountを保存することだけです。私の2次元配列を使って作業する

私が正確にしたいことは、ユーザーからの入力を受け入れることです:ジョンジョン10は候補者の名前であり、10は彼に与えたい投票です。だから私は{John、10}を私の配列に保存する必要があります。しかし、この後、私のプログラムは再びユーザに投票を求めるので、もしDoe 15の投票に入ると、そのエントリ{Doe、15}が配列に追加されます。ユーザーがVOTE John 2に入ると、私の配列を更新する必要があり、新しい値は{John、12}になります。

現在、私は2人のarraylists:候補者名とVoteCountを使用しています。私はペアリングのためにそのインデックスに依存しています。しかし、これは本当に信頼できるものではないので、私はこれを解決するための別の方法を見つけようとしています。しかし、私は多次元配列の大きなファンではありません。

誰かがこれを達成するための良い方法を教えてもらえますか?

答えて

3

Associative Arrayを使用してください。 C#の場合、そのようなコレクションはDictionaryです。

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

あなたはexisiting投票に追加したい場合は、既存の値があるかどうかを確認する必要があります。

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; 
    } 
} 
1

名前とVoteCountという2つのプロパティで構造体/クラスを定義しないのはなぜですか。次に、配列が1つだけ必要です。

EDIT:

あなたが候補に追加する追加の操作やプロパティがあるかもしれないので、私はこのことを示唆しました。必要なのは、これらの2つの値の間の関連付けであれば、辞書が正しい解決策です。

0

CandidateVotesというクラスを作成し、List<CandidateVotes>コレクションにそれを格納します。

public class CandidateVotes 
{ 
    public string Name {get; set;} 
    public int Votes {get; set;} 
} 
0
Dictionary<string, int> is your friend 
1

ここははるかに良い解決策のように聞こえるDictionary<TKey, TValue>を使用することです。辞書/ハッシュテーブルは、値(投票数)と指定されたキー(ユーザー名)をペアにするシナリオに最適です。それは非常に簡単で、更新と参照のシナリオ

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; 
    } 
} 
0

これはDictionary<T,U>のための良い候補のような音になります。この場合は、Dictionary<string,int>となり、キーは候補になり、値は投票数になります。

// 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); 
} 
5
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; 
    } 
1

文字列(名前)からint(投票)までの辞書を使うことができます。これはあなたに{名前、票}のペアと素敵なクイックルックアップを与えます

関連する問題