指定的强制转换无效,从锯齿状数组中获取所有值

时间:2016-06-11 11:23:16

标签: c# jagged-arrays

指定演员表无效,请告诉我从数组中存储的所有值中获取最大/最小值的方法,而不是从特定索引中获取。

int max = jArray.Cast<int>().Max(); 
                System.Console.Write("\n\n Max marks:" + max );

锯齿状阵列声明:

 string TotalStudents;

        System.Console.Write("Enter the Total No. Of Students:");
        TotalStudents = Console.ReadLine();

        int value;
        bool result = int.TryParse(TotalStudents, out value);

        JaggedArray jag = new JaggedArray(value);

        int[][] jArray = new int[jag.noOfStudents][];



        for (int i = 0; i < jag.noOfStudents; i++)
        {


            System.Console.Write("Enter the Total No. Of Subjects of Student:" + i + ":\t");
            string TotalSubjects = Console.ReadLine();

            int Subjectvalue;
            bool Sresult = int.TryParse(TotalSubjects, out Subjectvalue);
            jArray[i] = new int[Subjectvalue];

            for (int a = 0; a < Subjectvalue; a++)
            {
                System.Console.Write("\nEnter the marks obtained of subject:" + a + " of student " + i + ":\t");
                string TotalMarks = Console.ReadLine();

                int Marksvalue;
                bool Mresult = int.TryParse(TotalMarks, out Marksvalue);
                jArray[i][a] = Marksvalue;

            }

1 个答案:

答案 0 :(得分:1)

JArray是一个锯齿状数组(数组数组),这就是为int特定转换为无效的原因。

我建议使用SelectMany展平结构并查找Max

int max = jArray.SelectMany(x=>x.ToArray()).Max();