你如何链接两个数组?

时间:2022-03-06 23:07:19

I'm in a basic programming class, and everything is done in pseudo code.

我在一个基本的编程类中,一切都是用伪代码完成的。

My question is this: How do you link two arrays?

我的问题是:你如何链接两个数组?

I have a single-dimensional array that lists students names, and I have a two-dimensional array that lists the top eight scores of each student...this is all fine and dandy, but now I need to sort the arrays by the students name. I'm poked around online and read through the books chapter twice, it only briefly mentions linking two arrays but shows no examples.

我有一个列出学生姓名的单维数组,我有一个二维数组,列出了每个学生的前八个分数...这一切都很好,但现在我需要由学生对数组进行排序名称。我在网上闲逛并阅读了两次书籍章节,它只是简单地提到了连接两个数组,但没有显示任何例子。

If it's any help, we are using bubble-sorting, and that is what I am fairly familiar with...I can sort the names, that's the easy part, but I don't know how to sort the grades so they do not go out of order.

如果它有任何帮助,我们正在使用泡泡分类,这是我非常熟悉的......我可以对名称进行排序,这是最简单的部分,但我不知道如何对等级进行排序,因此它们不会出故障。

Thanks for the input!

感谢您的投入!

Sidenote: I got it figured out! I ended up doing how Greg Hewgill had mentioned. As I put in my comment to his suggestion, I started randomly throwing in lines of code until that idea hit me...it doesn't look pretty (one module swapped the names, another to swap the grades, and a third even then to swap the individual students grades earlier on in a multidimensional array), but it indeed seemed to work...no way to test it in a language as I have no compiler nor have I enough knowledge to make the pseudo code into actual code if I were to download one, but it sounds really good on the paper I typed it out on!

旁注:我明白了!我最终做了Greg Hewgill提到的事情。当我对他的建议发表评论时,我开始随机抛出一行代码,直到这个想法击中我......它看起来并不漂亮(一个模块交换名称,另一个交换等级,第三个甚至然后在多维数组中交换各个学生的成绩),但它确实似乎有效...没有办法用语言测试它,因为我没有编译器,也没有足够的知识将伪代码转换为实际代码我要下载一个,但是在我打字的纸上听起来真的很棒!

As I also mentioned in the note, I do thank everyone for their speedy and helpful insight, I actually didn't even think I'd get a reply tonight, thank you everyone again for all your help!

正如我在说明中所提到的,我感谢大家的快速和有益的见解,我实际上甚至不认为我今晚会得到答复,再次感谢大家的帮助!

Jeffrey

4 个解决方案

#1


1  

What you may want to do is the following: As you're sorting the names and you have to swap two positions, do the same swap in the array of scores. That way, all changes that you make to the names array will be reflected in the scores array. When you're done, the scores will be in the same sorted order as the names are.

您可能想要做的是:当您对名称进行排序并且必须交换两个位置时,在分数数组中执行相同的交换。这样,您对names数组所做的所有更改都将反映在score数组中。完成后,分数将与名称的排序顺序相同。

There are more effective ways of doing this with different data structures, as other comments will show.

正如其他评论所显示的那样,有更多有效的方法可以使用不同的数据结构。

#2


2  

Define a simple Student class like this:

像这样定义一个简单的Student类:

public class Student : IComparable<Student>
{
    public string Name { get; set; }
    public int[] Scores { get; set; }

    #region IComparable<Student> Members

    public int CompareTo(Student other)
    {
        // Assume Name cannot be null
        return this.Name.CompareTo(other.Name);
    }

    #endregion
}

then even simpler

然后更简单

    var students = new[] {
        new Student(){ Name = "B", Scores = new [] { 1,2,3 } },
        new Student(){ Name = "C", Scores = new [] { 3,4,5 } },
        new Student(){ Name = "A", Scores = new [] { 5,6,7 } }
    };

    Array.Sort(students);

will do the work for you.

会为你做的工作。

#3


0  

Your premise is wrong. You shouldn't have two array in the first place.

你的前提是错的。你不应该首先有两个数组。

You should have one array of objects, each of which holds a student's name and his scores:

你应该有一个对象数组,每个对象都有一个学生的名字和他的分数:

public class Record
{
    public string Student;
    public int[] Scores;
} 

#4


0  

Two approaches: first, when sorting the names, each time you exchange two names, exchange the rows (or columns or whatever you want to call them) of scores in the same positions. At the end, the scores should still be in sync with the names.

两种方法:首先,在对名称进行排序时,每次交换两个名称时,在相同位置交换分数的行(或列或任何您想要称之为的内容)。最后,分数仍应与名称同步。

Second, instead of sorting the names, create a third array that will contain the indexes into either of the other two arrays, initially 0 through n-1, but then sorted, comparing name[a] and name[b], instead of sorting the names array itself.

其次,不是对名称进行排序,而是创建第三个数组,该数组将索引包含在其他两个数组中的任何一个中,最初为0到n-1,但随后进行排序,比较名称[a]和名称[b],而不是排序名称数组本身。

#1


1  

What you may want to do is the following: As you're sorting the names and you have to swap two positions, do the same swap in the array of scores. That way, all changes that you make to the names array will be reflected in the scores array. When you're done, the scores will be in the same sorted order as the names are.

您可能想要做的是:当您对名称进行排序并且必须交换两个位置时,在分数数组中执行相同的交换。这样,您对names数组所做的所有更改都将反映在score数组中。完成后,分数将与名称的排序顺序相同。

There are more effective ways of doing this with different data structures, as other comments will show.

正如其他评论所显示的那样,有更多有效的方法可以使用不同的数据结构。

#2


2  

Define a simple Student class like this:

像这样定义一个简单的Student类:

public class Student : IComparable<Student>
{
    public string Name { get; set; }
    public int[] Scores { get; set; }

    #region IComparable<Student> Members

    public int CompareTo(Student other)
    {
        // Assume Name cannot be null
        return this.Name.CompareTo(other.Name);
    }

    #endregion
}

then even simpler

然后更简单

    var students = new[] {
        new Student(){ Name = "B", Scores = new [] { 1,2,3 } },
        new Student(){ Name = "C", Scores = new [] { 3,4,5 } },
        new Student(){ Name = "A", Scores = new [] { 5,6,7 } }
    };

    Array.Sort(students);

will do the work for you.

会为你做的工作。

#3


0  

Your premise is wrong. You shouldn't have two array in the first place.

你的前提是错的。你不应该首先有两个数组。

You should have one array of objects, each of which holds a student's name and his scores:

你应该有一个对象数组,每个对象都有一个学生的名字和他的分数:

public class Record
{
    public string Student;
    public int[] Scores;
} 

#4


0  

Two approaches: first, when sorting the names, each time you exchange two names, exchange the rows (or columns or whatever you want to call them) of scores in the same positions. At the end, the scores should still be in sync with the names.

两种方法:首先,在对名称进行排序时,每次交换两个名称时,在相同位置交换分数的行(或列或任何您想要称之为的内容)。最后,分数仍应与名称同步。

Second, instead of sorting the names, create a third array that will contain the indexes into either of the other two arrays, initially 0 through n-1, but then sorted, comparing name[a] and name[b], instead of sorting the names array itself.

其次,不是对名称进行排序,而是创建第三个数组,该数组将索引包含在其他两个数组中的任何一个中,最初为0到n-1,但随后进行排序,比较名称[a]和名称[b],而不是排序名称数组本身。