簡體   English   中英

指定的轉換無效,從鋸齒狀數組中獲取所有值

[英]Specified cast is not valid, getting all values from jagged array

指定的強制轉換無效,請告訴我從存儲在數組中的所有值而不是從特定索引中獲取最大值/最小值的任何方法。

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;

            }

JArray是一個鋸齒狀的數組(Array of Arrays),這就是為什么對int的特定JArray無效。

我建議使用SelectMany展平結構並尋找Max

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM