无法将 int 隐式转换为 int[]
Cannot Implicitly Convert int to int[]
有一些解决方案,但 none 对我有用。至于我的问题,我正在构建多个阵列。这是我的变量和我遇到问题的代码:
static int numOfEmployees;
static string[] nameArray;
static int[] idArray, deptArray;
static double[] payArray, hoursArray;
static void InputEmployeeData()
{
int i;
string[] words;
numOfEmployees = Int32.Parse(fileIn.ReadLine());
idArray = new int[numOfEmployees + 1];
nameArray = new string[numOfEmployees + 1];
deptArray = new int[numOfEmployees + 1];
payArray = new double[numOfEmployees + 1];
hoursArray = new double[numOfEmployees + 1];
for (i = 1; i <= numOfEmployees; i++)
{
words = fileIn.ReadFields();
idArray = Int32.Parse(words[0]);
nameArray = words[1];
deptArray = Int32.Parse(words[2]);
payArray = Double.Parse(words[3]);
hoursArray = Double.Parse(words[4]);
}
}
在我的 for 循环下,我在每一行上都得到“无法将类型 int 隐式转换为 int[]。或者将 double 类型转换为 double[]。
我试过投射,但似乎失败了。
这是因为您正在尝试分配数组而不是分配其成员:
idArray = Int32.Parse(words[0]);
应该是
idArray[i] = Int32.Parse(words[0]);
等等。更好的是,创建具有 id
、name
、dept
等单独字段的 EmployeeData
class,并使用它代替并行数组:
class EmployeeData {
public int Id {get;}
public string Name {get;}
public int Dept {get;}
public double Pay {get;}
public double Hours {get;}
public EmployeeData(int id, string name, int dept, double pay, double hours) {
Id = id;
Name = name;
Dept = dept;
Pay = pay;
Hours = hours;
}
}
现在您可以创建一个 EmployeeData
的数组或列表,并在您阅读他们的信息时创建单个员工:
var employee = new EmployeeData[numOfEmployees];
// Index i starts from 0, not from 1
for (i = 0; i < numOfEmployees; i++) {
words = fileIn.ReadFields();
var id = Int32.Parse(words[0]);
var name = words[1];
var dept = Int32.Parse(words[2]);
var pay = Double.Parse(words[3]);
var hours = Double.Parse(words[4]);
employee[i] = new EmployeeData(id, name, dept, pay, hours);
}
您正在尝试将一个 int 值分配给一个 int 数组。这会导致类型不匹配,从而导致编译器错误。
您真正想要做的是对数组进行索引并在指定索引处分配 int。
idArray[i] = int.Parse(words[0]);
有一些解决方案,但 none 对我有用。至于我的问题,我正在构建多个阵列。这是我的变量和我遇到问题的代码:
static int numOfEmployees;
static string[] nameArray;
static int[] idArray, deptArray;
static double[] payArray, hoursArray;
static void InputEmployeeData()
{
int i;
string[] words;
numOfEmployees = Int32.Parse(fileIn.ReadLine());
idArray = new int[numOfEmployees + 1];
nameArray = new string[numOfEmployees + 1];
deptArray = new int[numOfEmployees + 1];
payArray = new double[numOfEmployees + 1];
hoursArray = new double[numOfEmployees + 1];
for (i = 1; i <= numOfEmployees; i++)
{
words = fileIn.ReadFields();
idArray = Int32.Parse(words[0]);
nameArray = words[1];
deptArray = Int32.Parse(words[2]);
payArray = Double.Parse(words[3]);
hoursArray = Double.Parse(words[4]);
}
}
在我的 for 循环下,我在每一行上都得到“无法将类型 int 隐式转换为 int[]。或者将 double 类型转换为 double[]。
我试过投射,但似乎失败了。
这是因为您正在尝试分配数组而不是分配其成员:
idArray = Int32.Parse(words[0]);
应该是
idArray[i] = Int32.Parse(words[0]);
等等。更好的是,创建具有 id
、name
、dept
等单独字段的 EmployeeData
class,并使用它代替并行数组:
class EmployeeData {
public int Id {get;}
public string Name {get;}
public int Dept {get;}
public double Pay {get;}
public double Hours {get;}
public EmployeeData(int id, string name, int dept, double pay, double hours) {
Id = id;
Name = name;
Dept = dept;
Pay = pay;
Hours = hours;
}
}
现在您可以创建一个 EmployeeData
的数组或列表,并在您阅读他们的信息时创建单个员工:
var employee = new EmployeeData[numOfEmployees];
// Index i starts from 0, not from 1
for (i = 0; i < numOfEmployees; i++) {
words = fileIn.ReadFields();
var id = Int32.Parse(words[0]);
var name = words[1];
var dept = Int32.Parse(words[2]);
var pay = Double.Parse(words[3]);
var hours = Double.Parse(words[4]);
employee[i] = new EmployeeData(id, name, dept, pay, hours);
}
您正在尝试将一个 int 值分配给一个 int 数组。这会导致类型不匹配,从而导致编译器错误。
您真正想要做的是对数组进行索引并在指定索引处分配 int。
idArray[i] = int.Parse(words[0]);