【问题标题】:Is there a way to loop through an array, edit the contents of an array in C#, and the loop through it again to show the edited contents?有没有办法遍历数组,在 C# 中编辑数组的内容,然后再次循环以显示编辑后的内容?
【发布时间】:2011-09-25 10:30:12
【问题描述】:

我正在尝试遍历一系列产品。我想知道是否可以更改数组的一个元素,然后再次循环遍历它以将其与编辑后的元素一起显示。

代码如下:

public enum ProductStatus
{
    ForSale,
    Sold,
    Shipped
}

class Product
{
    private string description;
    private decimal price;
    private ProductStatus status;

    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    public decimal Price
    {
        get { return price; }
        set { price = value; }
    }

    public ProductStatus Status
    {
        get { return status; }
        set { status = 0; }
    }

然后实例化数组:

Product[] products = new Product[3];
products[0] = new Product() { Description = "Dress", Price = 69.99M };
products[1] = new Product() { Description = "Hat", Price = 5.95M};
products[2] = new Product() { Description = "Slacks", Price = 32.00M};

覆盖ToString() 方法后,我像这样循环遍历它:

ProductStatus status = ProductStatus.ForSale;
ProductStatus nextStatus = status + 1;

for (int i = 0; i < products.Length; i++)
{
    Product p = products[i];
    Console.WriteLine("{0}: " + p.ToString(), status.ToString());
}

我想使用 nextStatus 将第一个索引的 ProductStatus 从“ForSale”更改为“Sold”(然后是“Shipped”),然后再次循环遍历数组以显示更改。如何做到这一点?

【问题讨论】:

  • 不清楚是什么问题。只需循环两次(或 3x、4x、...)

标签: c# .net arrays loops enums


【解决方案1】:

虽然您的问题有点不清楚,但我想这就是您想要的(下面的代码测试)。

private void ChangeStatus(ProductStatus initialStatus, Product[] products)
{
    ProductStatus nextStatus = initialStatus + 1;
    foreach (var p in products)
    {
        p.Status = nextStatus;
    }
}

private void ShowProducts(Product[] products)
{
    foreach (var p in products)
    {
        Console.WriteLine("{0}: " + p.ToString(), status.ToString());
    }
}

输出可能如下所示(假设ForSale为初始状态):

P1(之前): 待售 - P1(之后)已售
P2(之前): Sold - P2(之后): Sold

无论如何,您确定要让您的应用程序以这种方式运行吗?在我看来,您要实现的目标略有不同,如下所示:

private void ChangeStatus(Product[] products)
{
    foreach (var p in products)
    {
        p.Status = p.Status + 1;
    }
}

在这种情况下,输出将如下:

P1(之前): 待售 - P1(之后)已售
P2(之前): 已售出 - P2(之后): 已发货

希望对你有帮助。

【讨论】:

    【解决方案2】:

    如果您的问题是“我如何确定更改的项目并查看它们”,您可以轻松地使用它们的后缀列表:

    private List<int> indexes = new List<int>();
    
    Loop through items
    {
     if (item is changed)
      indexes.Add(item's index);
    }
    

    【讨论】:

      【解决方案3】:

      为什么不创建一个包含已更改状态的新集合,例如:

          ProductStatus status = ProductStatus.ForSale;
          ProductStatus nextStatus = status + 1;
          var statusChangesList = new List<KeyValuePair<Product, Status>>();
          foreach (var product in products)
          {
              statusChangesList.Add(new KeyValuePair<Product, Status>(product, product.Status));
              product.Status = nextStatus;
          }
      
          foreach (var statusChange in statusChangesList)
          {
              Console.WriteLine("Product: " + statusChange.key + " changed status from: " + statusCahnge.value);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-21
        • 2019-03-22
        • 2015-07-07
        • 1970-01-01
        • 2013-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多