C# 中的锯齿状数组

发布于 2024-09-03 18:01:22 字数 712 浏览 6 评论 0原文

我试图将整数数组存储在锯齿状数组中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赤濁 2024-09-10 18:01:23

一种更好的、更面向对象的方法是创建一个具有 Scores 属性的 Customer 类:

public class Customer
{
    public Customer()
    {
        this.Scores = new List<int>();
    }

    public IList<int> Scores { get; private set; }
}

由于事实证明每个客户只有一个分数,因此更正确的 Customer 类可能如下所示:

public class Customer
{
    public int Score { get; set; }
}

如果您以后不需要更新 Score 属性,您可以考虑将其设置为只读。

A better, more object-oriented approach would be to create a Customer class with a Scores property:

public class Customer
{
    public Customer()
    {
        this.Scores = new List<int>();
    }

    public IList<int> Scores { get; private set; }
}

Since it turns out that there is only one score per customer, a more correct Customer class might look like this:

public class Customer
{
    public int Score { get; set; }
}

You might consider making the Score property read-only if you don't need to be able to update it afterwards.

若无相欠,怎会相见 2024-09-10 18:01:23

你知道从什么尺寸开始吗?如果是这样,您可以这样做:

int[] customerIds = new int[size];
int[] scores = new int[size];
int index = 0;
while (dr5.Read())
{
    customerIds[index] = ...;
    scores[index] = ...;
    index++;
}
int[][] combined = { customerIds, scores };

但是,我建议您重新考虑。听起来您确实想将客户 ID 与分数关联起来...因此创建一个类来执行此操作。然后你可以这样做:

List<Customer> customers = new List<Customer>();
while (dr5.Read())
{
    int customerId = ...;
    int score = ...;
    Customer customer = new Customer(customerId, score);
    customers.Add(customer);
}

Do you know the size to start with? If so, you could do:

int[] customerIds = new int[size];
int[] scores = new int[size];
int index = 0;
while (dr5.Read())
{
    customerIds[index] = ...;
    scores[index] = ...;
    index++;
}
int[][] combined = { customerIds, scores };

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:

List<Customer> customers = new List<Customer>();
while (dr5.Read())
{
    int customerId = ...;
    int score = ...;
    Customer customer = new Customer(customerId, score);
    customers.Add(customer);
}
心不设防 2024-09-10 18:01:23

作为使用数组的另一种想法:

如果它是一对一映射,您可以使用字典进行临时存储,如下所示:

var scores = new Dictionary<int, int>();
while (dr5.Read())  
{  
   scores.Add(int.Parse(dr5["customer_id"].ToString()), int.Parse(dr5["score"].ToString()));
}  

否则您可以创建一个类客户并从中创建一个列表。

As an alternative idea of using arrays:

If it is a one to one mapping you can use Dictionary for temporary storage like this:

var scores = new Dictionary<int, int>();
while (dr5.Read())  
{  
   scores.Add(int.Parse(dr5["customer_id"].ToString()), int.Parse(dr5["score"].ToString()));
}  

Else you can create a class customer and make a list out of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文