简体   繁体   English

如何在一个维度数组中添加更多“元素”或“维度”?

[英]How do I add more “elements” or “dimensions” to a single dimension array?

I hope I am wording my question the right way, and using the correct terminology. 我希望我以正确的方式并使用正确的术语来表达我的问题。 I have an array of student names, arrSummary , that is distinct (no duplicates). 我有一组学生姓名arrSummary ,它们是不同的(无重复)。

Then, I have another array, arrTest1 , that contains a list of the same students' names and their test scores on Test1. 然后,我有另一个数组arrTest1 ,其中包含相同学生的姓名及其在Test1上的考试成绩的列表。 Then I have a third array, arrTest2 , that contains a list of their names and their test scores on Test2. 然后,我有了第三个数组arrTest2 ,其中包含它们的名称和在Test2上的测试分数的列表。

arrTest1 and arrTest2 are such that arrTest1[0].Name = "Bob" and arrTest1[0].Score = "98" . arrTest1arrTest2使得arrTest1[0].Name = "Bob"arrTest1[0].Score = "98"

How do I make the array, arrSummary , such that arrSummary[0].Name = "Bob" , arrSummary[0].Score1 = "98" , arrSummary[0].Score2 = "76" ? 如何制作数组arrSummary ,使arrSummary[0].Name = "Bob"arrSummary[0].Score1 = "98"arrSummary[0].Score2 = "76"

There are different types in the array (string, integer, integer). 数组中有不同类型(字符串,整数,整数)。

var arrSummary = arrTest1.Join(arrTest2, person => person.Name, person => person.Name
    , (first, second) => new
    {
        Name = first.Name,
        Score1 = first.Score,
        Score2 = second.Score,
    })
    .ToArray();

Note that this is querying the first two arrays and creating a brand new array. 请注意,这是查询前两个数组并创建一个全新的数组。 Here I used an anonymous type for the query result; 在这里,我为查询结果使用了匿名类型。 if the query results will be used outside of this method you'll want to make a new class with those 3 properties and project the results to instances of that type (just put that type after new ). 如果查询结果将在此方法之外使用,则需要创建一个具有这3个属性的新类,并将结果投影到该类型的实例上(只需将该类型放在new之后)。

You can't change the dimensions, the type, or even the size of an array once it is created. 创建数组后,您将无法更改其尺寸,类型甚至大小。 If the summary array already is of a type that has all three properties then you could in theory go through and populate it, but it would probably be easier to just generate the summary array from scratch, which is why that's what I've done here. 如果摘要数组已经具有所有三个属性的类型,那么理论上您可以遍历并填充它,但是仅从头开始生成摘要数组可能会更容易,这就是我在这里所做的原因。

Use a single list or array that stores data as fields in a class rather than three different arrays, like so: 使用单个列表或数组将数据存储为类中的字段,而不要使用三个不同的数组,如下所示:

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;

You need to create a type and use that in your array: 您需要创建一个类型并在您的数组中使用它:

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

OR 要么

You could do this the RIGHT way: 您可以以正确的方式执行此操作:

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

Then you should have a List<Student> instead of your summary array thing. 然后,您应该有一个List<Student>而不是您的摘要数组。

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();

(Note this only works if your arrTest arrays don't have any missing entries. ie, there's no names in arrSummary that aren't in arrTest1 or arrTest2.) (请注意,这仅在您的arrTest数组没有任何缺失条目的情况下才有效。也就是说,arrSummary中没有不存在于arrTest1或arrTest2中的名称。)

Create a class or struct that contains the data you want to store and create an array of those objects! 创建一个包含要存储的数据的类或结构,并创建这些对象的数组! Better yet you may consider using a List! 更好的是,您可以考虑使用列表!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM