使用字符串进行OOP数组过滤?

时间:2013-11-28 22:15:16

标签: c# arrays oop filtering

所以我对C#OOP很陌生,我只是在大学时就掌握了这种编程风格,我遇到了阵列。现在,我理解如何创建它们以及它们的一般用途,但我不明白我将如何使用字符串过滤它们。

对于一个小故事,这是我被要求做的一部分;

在我的项目的一部分中,我需要创建三个数组,设置主题信息,然后使用显示方法将其输出给用户。我还被要求使用_findExamIndex(根据给出的考试ID搜索考试数据)列出考试,然后使用_findStudentIndex(为学生搜索学生列表)以选择个别学生结果。

我遇到的问题是使用字符串_topicName过滤_exams数组的结果,我不确定如何做到这一点,因为我是新手使用数组作为存储数据的方式,并且非常感谢一点帮助,非常感谢。

(方法代码)

public void viewTopicsResults(string _topicName,List<Results> _results, List<Student> _students, Exam[] _exams, DisplayClass.Display _theDisplay)
{
        int[] columnWidths = { 3, 20 };
        char[] charactersInTheSpaces = { ' ', ' ' };
        string[] topicInfo = new string[6];

        topicInfo[0] = "Student";
        topicInfo[1] = "Subject";
        topicInfo[2] = "Topic";
        topicInfo[3] = "Style";
        topicInfo[4] = "Date";
        topicInfo[5] = "Score";


        //Ordering the table by topic.      
        _theDisplay.writeLineWithTab(topicInfo, columnWidths, charactersInTheSpaces);


        foreach(Results result in _results)
        {
            int examID = _theDisplay.userInputAsInteger("Select a result");
            _findExamIndex(examID, _exams);
             int studentID = _theDisplay.userInputAsInteger("Please select a users result");
            _findStudentIndex(studentID, _students);

             //filter on the _exams' Topic == '_topicName'

        }
}

(此方法将由main方法调用)

2 个答案:

答案 0 :(得分:2)

只做这样的事情

    foreach(Results result in _results)
    {
        int examID = _theDisplay.userInputAsInteger("Select a result");
        _findExamIndex(examID, _exams);
         int studentID = _theDisplay.userInputAsInteger("Please select a users result");
        _findStudentIndex(studentID, _students);

         //filter on the _exams' Topic == '_topicName'
         exam[]  exams  = FindExamByTopic("youtopic",_exams)

    }


  private Exam[] FindExamByTopic(string topic, Exam[] _exams)
    {
         return _exams.Where(ex => ex.Topic == topic).ToArray();  
    }


  //here without using  linq 

     private Exam[] FindExamByTopicSimple(string topic, Exam[] _exams)
    {
        List<Exam>  exams = new List<Exam>();

        for (int i = 0; i < _exams.Length; i++)
        {
            if (_exams[i].Topic == topic)
            {
                exams.Add(_exams[i]); 
            }
        }
        return exams.ToArray();   
    }

希望这个帮助

答案 1 :(得分:0)

非常感谢,经过深思熟虑,改变和调整代码,我已经提出了这个解决方案;

char[] padding = { ' ', ' ', ' ', ' ', ' ', ' ' };
        int[] widths = { 20, 9, 26, 14, 11, 4 };
        string[] topicInfo = { "Student", "Subject", "Topic", "Style", "Date", "Score" };

        _theDisplay.clearScreen();
        _theDisplay.writeLineWithTab(topicInfo, widths, padding);

        for (int i = 0; i < _results.Count; i++)
        {
            int examIndex = _findExamIndex(_results[i].ExamRegisterNos, _exams);
            int studentIndex = _findStudentIndex(_results[i].StudentIDNumber, _students);

            if (_exams[examIndex].Topic == _topicName)
            {
                topicInfo[0] = Convert.ToString(_students[studentIndex].studentName());
                topicInfo[1] = _exams[examIndex].Subject;
                topicInfo[2] = _exams[examIndex].Topic;
                topicInfo[3] = _exams[examIndex].StyleOfExam;
                topicInfo[4] = Convert.ToString(_results[i].TakenOn.ToShortDateString());
                topicInfo[5] = Convert.ToString(_results[i].IntGradeOutOf100 + "%");

                _theDisplay.writeLineWithTab(topicInfo, widths, padding);
            }
        }

感谢您的帮助,感激不尽。