C# 数组的第一个索引返回IndexOutOfRangeException
我正在编写一个Unity脚本,它读取CSV文件(2D和所有数字),将其分解为附加到2D浮点数组的浮点。这是我的密码:C# 数组的第一个索引返回IndexOutOfRangeException,c#,multidimensional-array,indexoutofrangeexception,C#,Multidimensional Array,Indexoutofrangeexception,我正在编写一个Unity脚本,它读取CSV文件(2D和所有数字),将其分解为附加到2D浮点数组的浮点。这是我的密码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class LoadCalibration : MonoBehaviour { public float[,] pc_array; // Reconstructed PC coefficient MD
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadCalibration : MonoBehaviour
{
public float[,] pc_array; // Reconstructed PC coefficient MD array (PCs as rows and variables as columns)
// Start is called before the first frame update
void Start()
{
// PC COEFFICIENTS
pc_array = new float[20, 20];
Debug.Log(pc_array);
TextAsset pc_data = Resources.Load<TextAsset>("pc_coeff"); //Data is in as variables x PCs
string[] variable = pc_data.text.Split(new char[] { '\n' }); // split pc_data into rows(each row is one variable, for all PCs)
for (int i = 0; i < variable.Length - 1; i++)
{
string[] pc = variable[i].Split(new char[] { ',' }); // delegate each variable to a pc
Debug.Log(i);
for (int j = 0; j < pc.Length; i++)
{
Debug.Log(j);
pc_array[j,i] = float.Parse(pc[j]); // Load float value into the pc_coeff MD array
}
}
}
}
使用Debug.Log()
我已经计算出错误发生在I=0和j=0(数组的第一个索引)处,即使我将其声明为20 x 20数组。我是C#新手,所以错误可能很明显,但我无法解决。任何帮助都将不胜感激
我使用了Debug.Log()
来评估其余代码是否正常工作(即读取CSV文件并将每个字符串条目转换为单个浮点)。for(int j=0;j
改为
for (int j = 0; j < pc.Length; j++)
for(int j=0;j
用于(int j=0;j
改为
for (int j = 0; j < pc.Length; j++)
for(int j=0;j
执行Debug.Log()
时,还应包括变量的名称。这可能会让问题直接跳出来。在执行Debug.Log()
时,还应该包括变量的名称。这可能会让问题直接跳到你身上。
for (int j = 0; j < pc.Length; j++)