如何链接两个数组?

发布于 2024-07-09 12:07:28 字数 581 浏览 12 评论 0原文

我正在上基础编程课,一切都是用伪代码完成的。

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

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

如果有帮助的话,我们正在使用冒泡排序,这就是我相当熟悉的...我可以对名称进行排序,这是简单的部分,但我不知道如何对成绩进行排序,所以他们不出故障。

感谢您的投入!

旁注:我明白了! 我最终按照格雷格休吉尔提到的那样做了。 当我对他的建议发表评论时,我开始随机地添加几行代码,直到这个想法击中了我......它看起来不太漂亮(一个模块交换了名称,另一个模块交换了成绩,甚至还有第三个模块)在多维数组中交换单个学生的成绩),但它似乎确实有效......无法用语言测试它,因为我没有编译器,也没有足够的知识将伪代码变成实际代码,如果我本想下载一个,但在我打出来的纸上听起来真的很棒!

正如我在注释中提到的,我确实感谢大家快速而有帮助的见解,实际上我什至没想到今晚会得到回复,再次感谢大家的帮助!

杰弗里

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!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

另类 2024-07-16 12:07:28

像这样定义一个简单的 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
}

然后就

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

可以更简单地为您完成工作。

Define a simple Student class like this:

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.

长途伴 2024-07-16 12:07:28

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

正如其他评论将表明的那样,对于不同的数据结构,有更有效的方法来做到这一点。

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.

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

枕花眠 2024-07-16 12:07:28

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

您应该有一组对象,每个对象都包含一个学生的姓名和分数:

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

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;
} 
云巢 2024-07-16 12:07:28

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文