C# 中的锯齿状数组
我试图将整数数组存储在锯齿状数组中:
while (dr5.Read())
{
customer_id[i] = int.Parse(dr5["customer_id"].ToString());
i++;
}
dr5 是一个数据读取器。我将 customer_id 存储在一个数组中,我还想将分数存储在另一个数组中。 我想在 while 循环中有类似下面的内容
int[] customer_id = { 1, 2 };
int[] score = { 3, 4};
int[][] final_array = { customer_id, score };
有人可以帮助我吗? 编辑: 这就是我尝试过的。没有显示任何值。
customer_id = new int[count];
score = new int[count];
int i = 0;
while (dr5.Read())
{
customer_id[i] = int.Parse(dr5["customer_id"].ToString());
score[i] = 32;
i++;
}
int[][] final = { customer_id, score };
return this.final;
Im trying to store to array of ints in a jagged array:
while (dr5.Read())
{
customer_id[i] = int.Parse(dr5["customer_id"].ToString());
i++;
}
dr5 is a datareader. I am storing the customer_id in an array, i also want to store scores in another array.
I want to have something like below within the while loop
int[] customer_id = { 1, 2 };
int[] score = { 3, 4};
int[][] final_array = { customer_id, score };
Can anyone help me please ?
EDIT:
this is what i have tried. No values are being displayed.
customer_id = new int[count];
score = new int[count];
int i = 0;
while (dr5.Read())
{
customer_id[i] = int.Parse(dr5["customer_id"].ToString());
score[i] = 32;
i++;
}
int[][] final = { customer_id, score };
return this.final;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种更好的、更面向对象的方法是创建一个具有 Scores 属性的 Customer 类:
由于事实证明每个客户只有一个分数,因此更正确的 Customer 类可能如下所示:
如果您以后不需要更新 Score 属性,您可以考虑将其设置为只读。
A better, more object-oriented approach would be to create a Customer class with a Scores property:
Since it turns out that there is only one score per customer, a more correct Customer class might look like this:
You might consider making the Score property read-only if you don't need to be able to update it afterwards.
你知道从什么尺寸开始吗?如果是这样,您可以这样做:
但是,我建议您重新考虑。听起来您确实想将客户 ID 与分数关联起来...因此创建一个类来执行此操作。然后你可以这样做:
Do you know the size to start with? If so, you could do:
However, I would advise you to rethink. It sounds like you really want to associate a customer ID with a score... so create a class to do so. Then you can do:
作为使用数组的另一种想法:
如果它是一对一映射,您可以使用字典进行临时存储,如下所示:
否则您可以创建一个类客户并从中创建一个列表。
As an alternative idea of using arrays:
If it is a one to one mapping you can use Dictionary for temporary storage like this:
Else you can create a class customer and make a list out of it.