ทำงานกับอาร์เรย์สองมิติของฉัน

ฉันแค่อยากถามวิธีที่ดีที่สุดในการทำงานกับอาร์เรย์สองมิติ (2 คอลัมน์) ซึ่งจะจัดเก็บ: CandidateName และ VoteCount ตามลำดับ

สิ่งที่ฉันต้องการทำคือยอมรับข้อมูลจากผู้ใช้โดยพูดว่า: VOTE John 10 โดยที่ John คือชื่อของผู้สมัครและ 10 คือคะแนนโหวตที่เขาต้องการให้เขา ดังนั้นฉันจึงต้องเก็บ {John, 10} ไว้ในอาร์เรย์ของฉัน อย่างไรก็ตาม หลังจากนี้ โปรแกรมของฉันก็ขอให้ผู้ใช้ลงคะแนนอีกครั้ง ดังนั้นหากฉันป้อน VOTE Doe 15 รายการ {Doe, 15} จะถูกเพิ่มลงในอาร์เรย์ หากผู้ใช้ป้อน VOTE John 2 อาร์เรย์ของฉันต้องได้รับการอัปเดต ดังนั้นค่าใหม่จะเป็น {John, 12}

ขณะนี้ฉันใช้รายการอาร์เรย์สองรายการ: CandidateName และ VoteCount และฉันแค่ใช้ดัชนีในการจับคู่ อย่างไรก็ตาม สิ่งนี้ไม่น่าเชื่อถือจริงๆ ดังนั้นฉันจึงพยายามหาวิธีอื่นในการแก้ไขปัญหานี้ อย่างไรก็ตาม ฉันไม่ใช่แฟนตัวยงของอาร์เรย์หลายมิติ

ใครช่วยกรุณาชี้ให้ฉันทราบถึงวิธีที่ดีในการบรรลุเป้าหมายนี้


person Smiley    schedule 20.05.2011    source แหล่งที่มา


คำตอบ (6)


คุณควรใช้ Associative Array ในกรณีของ 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;
    }
}
person Paul Ruane    schedule 20.05.2011

ทำไมคุณไม่กำหนด struct/class ด้วยสองคุณสมบัติ Name และ VoteCount จากนั้นคุณต้องการเพียงอาร์เรย์เดียวเท่านั้น

แก้ไข:

ฉันแนะนำสิ่งนี้เนื่องจากอาจมีการดำเนินการหรือคุณสมบัติเพิ่มเติมที่คุณต้องการเพิ่มให้กับผู้สมัคร หากคุณต้องการเพียงการเชื่อมโยงระหว่างสองค่านี้ พจนานุกรมคือคำตอบที่ถูกต้อง

person Nathanael    schedule 20.05.2011

ดูเหมือนวิธีแก้ปัญหาที่ดีกว่ามากที่นี่คือการใช้ 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;
  }
}
person JaredPar    schedule 20.05.2011

คุณสามารถใช้พจนานุกรมตั้งแต่สตริง (ชื่อ) ไปจนถึง int (โหวต) ซึ่งจะให้คู่ {name, vote} และการค้นหาอย่างรวดเร็ว

person Neowizard    schedule 20.05.2011

สร้างคลาสชื่อ CandidateVotes และเก็บไว้ในคอลเลกชัน List<CandidateVotes>

public class CandidateVotes
{
    public string Name {get; set;}
    public int Votes {get; set;}
}
person Ken Smith    schedule 20.05.2011

ดูเหมือนจะเป็นตัวเลือกที่ดีสำหรับ 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);
}
person Reed Copsey    schedule 20.05.2011