【问题标题】:How do I add more "elements" or "dimensions" to a single dimension array?如何向一维数组添加更多“元素”或“维度”?
【发布时间】:2012-09-07 16:16:00
【问题描述】:

我希望我用正确的方式来表达我的问题,并使用正确的术语。我有一组学生姓名arrSummary,这是不同的(没有重复)。

然后,我有另一个数组 arrTest1,其中包含相同学生的姓名和他们在 Test1 上的考试成绩的列表。然后我有第三个数组arrTest2,其中包含他们的姓名和他们在 Test2 上的考试成绩的列表。

arrTest1arrTest2 是这样的 arrTest1[0].Name = "Bob"arrTest1[0].Score = "98"

如何制作数组arrSummary,使得arrSummary[0].Name = "Bob"arrSummary[0].Score1 = "98"arrSummary[0].Score2 = "76"

数组中有不同的类型(字符串、整数、整数)。

【问题讨论】:

  • 为什么没有学生班级的 arrSummat 列表?
  • 您可以使用 Linq Join 语句来组合两个列表。
  • 避免使用匈牙利符号(例如,在数组变量名称前加上“arr”)。
  • 嗨,大卫。你能解释一下为什么我应该避免使用匈牙利符号吗?我需要命令背后的原因/解释才能这样做。
  • 您是想彻底重构您的应用程序,以消除现有的三个数组,还是只是想创建第四个数组来组合现有三个数组中的数据?

标签: c# visual-studio-2010


【解决方案1】:
var arrSummary = arrTest1.Join(arrTest2, person => person.Name, person => person.Name
    , (first, second) => new
    {
        Name = first.Name,
        Score1 = first.Score,
        Score2 = second.Score,
    })
    .ToArray();

请注意,这是查询前两个数组并创建一个全新的数组。这里我对查询结果使用了匿名类型;如果查询结果将在此方法之外使用,您需要使用这 3 个属性创建一个新类并将结果投影到该类型的实例(只需将该类型放在 new 之后)。

一旦创建数组,您就无法更改其维度、类型甚至大小。如果摘要数组已经是具有所有三个属性的类型,那么理论上您可以遍历并填充它,但是从头开始生成摘要数组可能会更容易,这就是我在这里所做的原因.

【讨论】:

    【解决方案2】:

    使用单个列表或数组将数据存储为类中的字段,而不是三个不同的数组,如下所示:

    class TestResult {
        public String Name;
        public Int32 TestResult1;
        public Int32 TestResult2;
    }
    
    TestResult[] testResults = new TestResult[ numberOfStudents ];
    testResults[0].Name = "Mr. Foo bar";
    testResults[0].TestResult1 = 65;
    testResults[0].TestResult2 = 76;
    

    【讨论】:

      【解决方案3】:

      你需要创建一个类型并在你的数组中使用它:

      public class TestSummary {
        public string StudentName {get;set;}
        public int Score1 {get;set;}
        public int Score2 {get;set;}
      }
      

      你可以用正确的方式做到这一点:

      public class Score{
         public int TestId {get;set;}
         public int StudentId {get;set;}
         public int Score {get;set;}
      }
      
      public class Student{
         public int ID {get;set;}
         public string FirstName {get;set;}
         public string LastName {get;set;}
         public List<Score> Scores;
      }
      

      那么你应该有一个List&lt;Student&gt; 而不是你的摘要数组。

      【讨论】:

        【解决方案4】:
        var scores1 = arrTest1.ToDictionary(score => score.Name);
        var scores2 = arrTest2.ToDictionary(score => score.Name);
        var output = arrSummary.Select(name=>new {Name=name, Test1 = scores1[name], Test2 = scores2[name]).ToArray();
        

        (请注意,这仅在您的 arrTest 数组没有任何缺失条目时才有效。即 arrSummary 中没有不在 arrTest1 或 arrTest2 中的名称。)

        【讨论】:

        • 您需要将两个字典的值设置为Score 属性,或者您需要从Select 的类型中提取Score 属性。 (我更喜欢第一个选项)。
        【解决方案5】:

        创建一个包含您要存储的数据的类或结构,并创建这些对象的数组!更好的是,您可以考虑使用列表!

        public class Student
        {
            public string name {get;set;}
            public int score1 {get;set;}
            public int score2 {get;set;}
        }
        

        ...

        List <Student> = new List<Student>();
        List.Add({name="Len", score1=90, score2=100});
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多