簡體   English   中英

C#列表索引數組超出范圍

[英]C# Array of List Index out of bounds

我已經制作了一個程序,可以從文件中提取一些信息,對其進行一些操作,然后將其存儲回列表中。 通過此鏈接: c#中是否可能有二維列表? 我已經能夠創建一個包含適合我需求的列表的課程。 但是經過一些調試后,我發現我在每次循環迭代時都覆蓋了列表。 然后,我決定制作一個列表數組-單擊此鏈接: 如何在C#中創建List <int>數組?

創建一個列表數組,對其進行初始化並添加元素。 但是,當它需要移動到下一個列表位置時,就會拋出邊界外異常。

我已經嘗試了幾件事(閱讀了有關比賽狀況的信息),但沒有一件事奏效。 僅當我用代碼打開多個文件時,才會發生此問題; 否則,它會完美運行。

在當前文件的最后一次迭代中,xmldata拋出異常。 例如:選擇兩個文件,每個文件將添加五個元素。 在第一個文件的最后一個元素中,將引發異常,並且要在最后一個元素的位置添加數據。

附加信息:索引超出了數組的范圍。 (拋出異常)。

任何幫助將不勝感激。 非常感謝。

碼:

List<xmldata>[] finalcontent = new List<xmldata>[9999];
 finalcontent[listpos] = new List<xmldata>();//Initializing a list for each filename

                            foreach (Match m in matches)
                            {

                                Double[] numbers;
                                string aux;
                                aux = m.Groups[1].ToString();
                                aux = Regex.Replace(aux, @"\s+", "|");
                                string[] numbers_str = aux.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                                numbers = new Double[numbers_str.Length];
                                for (int j = 0; j < numbers.Length; j++)
                                {
                                    numbers[j] = Double.Parse(numbers_str[j], CultureInfo.InvariantCulture);
                                    //Converts each number on the string to a Double number, store it in a position
                                    //in the Double array
                                    numbers[j] = numbers[j] / 100; //Needed calculus
                                    numbers[j] = Math.Round(numbers[j], 3); //Storing numbers rounded
                                }
                                string values = String.Join(" ", numbers.Select(f => f.ToString()));

                                if (i <= colors_str.Length)
                                {
                                    finalcontent[listpos].Add(new xmldata//The exception is thrown right here
                                    {
                                        colorname = colors_str[i],
                                        colorvalues = values,

                                    });//Closing list add declaration
                                 }//Closing if
                                i++;
                            }//Closing foreach loop

鏈接到文件: https : //drive.google.com/file/d/0BwU9_GrFRYrTT0ZTS2dRMUhIWms/view?usp=sharing

數組是固定大小的,但是列表會隨着新項目的添加自動調整大小。

因此,相反,既然您仍在使用列表,為什么不使用列表列表呢?

List<List<int>> ListOfListsOfInt = new List<List<int>>();

然后,如果您確實絕對必須有一個數組,那么可以得到一個這樣的數組:

ListOfListsOfString.ToArray();
// Convert non-ascii characters to .
for (int jx = 0; jx < cnt; ++jx)
if (line[jx] < 0x20 || line[jx] > 0x7f) line[jx] = (byte)'.';

這是一個很大的例子,但是請檢查一下這個例子。 您在輸入語句之前增加了“ jx”,可能超出了cnt的范圍?

使用列表時-最好使用本機函數。

List<xmldata>[] finalcontent = new List<xmldata>(); 
......
 finalcontent[listpos] = new List<xmldata>(); insted of   var _tmpVariable = new List<xmldata>();//Initializing a list for each filename
......
 _tmpVariable.Add(new xmldata
                                    {
                                        colorname = colors_str[i],
                                        colorvalues = values,

                                    });//Closing list add declaration

                    fs.Close();//closing current file
                    listpos++;//Increment list position counter
                    finalcontent.Add(_tmpVariable); // add list into list

由於沒有異常詳細信息,因此很難找到引發異常的位置。 它可能是列表問題,字符串問題或其他(甚至是文件讀取問題),因此請使用當前的異常詳細信息進行更新。

嘗試更改以下內容:

if (i <= colors_str.Length) 

if (i < colors_str.Length). 

實際上,我確信這是問題所在。

這是因為引用從0開始,最后一個引用是長度-1,而不是長度。

暫無
暫無

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

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