【问题标题】:C# how to update a list using data from a fileC#如何使用文件中的数据更新列表
【发布时间】:2018-11-16 18:42:25
【问题描述】:

我正在处理一项需要从两个文本文件中读取输入的作业。根据第二个文件的数据修改第一个文件的数据,并将结果打印到第三个文件。

第一个文件是 Inventory 文件,其中包含 ID 号、产品名称和数量,格式如下:

123 |电视| 45
124 |电脑 | 52
125 |收音机 | 20

第二个文件包含ID号和数量变化:

123 | -8
124 | 7

在上一个问题中,我询问了如何将第一个文件的数据读入列表。我相信我已经在下面代码的 main 方法中实现了这一点。同样在下面的代码中,我有一个循环,它将从第二个文件中读取行并将 ID 号存储为updateID,并将数量存储为numUpdate。我想使用这些值来修改列表Inventory,但我不确定该怎么做。当我研究这个问题时,我的第一个想法是.IndexOf 方法,但我认为这行不通。使用第二个文件的数据更新Inventory 列表的过程是什么?当然,我最终还是想将更新列表打印到第三个文件中……但我现在并不真正关心。

class Program
{
    public class InventoryNode
    { 
        public int IDNumber;
        public string Name;
        public int Quantity;

        public InventoryNode()
        {
            IDNumber = 0;
            Name = " ";
            Quantity = 0;
        }

        public InventoryNode(int ID, string InvName, int Number)
        {
            IDNumber = ID;
            Name = InvName;
            Quantity = Number;
        }

        public override string ToString()   // This one is a freebie
        {
            return IDNumber + " | " + Name + " | " + Quantity;
        }
    }

    static void Main(String[] args)
    {
        int ID;
        string InvName;
        int Number;

        string updateID;
        int numUpdate;

        string line; 

        List<InventoryNode> Inventory = new List<InventoryNode>();
        InventoryNode Item = null;

        StreamReader f1 = new StreamReader(args[0]);
        StreamReader f2 = new StreamReader(args[1]);
        StreamWriter p = new StreamWriter(args[2]);

        while ((line = f1.ReadLine()) != null)
        {
            string[] currentLine = line.Split('|');
            {
                ID = Convert.ToInt16(currentLine[0]);
                InvName = currentLine[1];
                Number = Convert.ToInt16(currentLine[2]);                    
            }
            Item = new InventoryNode(ID, InvName, Number);
            Inventory.Add(Item);               
        }
        while ((line = f2.ReadLine()) != null)
        {
            string[] currentLine2 = line.Split('|');
            updateID = currentLine2[0];
            numUpdate = Convert.ToInt16(currentLine2[1]);
        }
        return;
    }
}

【问题讨论】:

  • intInt32,而不是 Int16

标签: c# arrays list file-io


【解决方案1】:

你可以这样做

Inventory.First(node => node.IDNumber == updateID).Quantity += numUpdate;

您阅读第二个文件的位置

编辑:我写这个假设你的第二个文件不包含无效的 id,你需要一个 nullcheck

【讨论】:

    【解决方案2】:

    你正朝着正确的方向前进。

    在解析第二个文件的内容时,您可以更新Inventory列表如下:

        while ((line = f2.ReadLine()) != null)
        {
            string[] currentLine2 = line.Split('|');
            updateID = currentLine2[0];
            numUpdate = Convert.ToInt32(currentLine2[1]);
    
            var inventoryNode = Inventory.Where(i=>i.ID == updateID).FirstOrDefault();
            if(inventoryNode != null)
            {
                inventoryNode.Number += numUpdate;
            }
        }
    

    这将更新列表Inventory 中的数据,因为这些是引用类型。

    【讨论】:

    • 我添加了这个以查看它是如何工作的,我在第 7 行的 i.ID 下收到一条错误消息。关于Program.InventoryNode 的东西没有ID 的定义。有没有办法解决这个问题?
    【解决方案3】:

    我不会将第一个文件存储在列表中,而是存储在Dictionary&lt;TKey, TValue&gt; 中。这允许您通过键查找项目。

    var Inventory = new Dictionary<int, InventoryNode>();
    
    ...
    
    Item = new InventoryNode(ID, InvName, Number);
    Inventory.Add(ID, Item);
    

    然后用

    查找一个项目
    while ((line = f2.ReadLine()) != null)
    {
        string[] currentLine2 = line.Split('|');
        int id = Convert.ToInt32(currentLine2[0]);
        numUpdate = Convert.ToInt32(currentLine2[1]);
        if (Inventory.TryGetValue(id, out var inventoryNode)) {
            inventoryNode.Quantity += numUpdate;
        }
    }
    

    请注意,在字典中查找比在列表中搜索要快得多。字典查找具有几乎恒定的时间 (O(1))。 IE。它不取决于存储的项目数量。而他的访问时间随着列表大小线性增长 (O(n))。

    另外,Convert.ToInt32 创建一个int,而Convert.ToInt16 创建一个short

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 2019-07-26
      相关资源
      最近更新 更多