【问题标题】:Console.WriteLine(ArrayList) wrong outputConsole.WriteLine(ArrayList) 输出错误
【发布时间】:2012-05-24 09:20:55
【问题描述】:

我正在尝试打印各种 foreach 循环的 ArrayList 的内容,但我得到的唯一结果是 String + System.Collections.ArrayList。

例如下面的代码:

ArrayList nodeList = new ArrayList();
foreach (EA.Element element in elementsCol)
{
    if ((element.Type == "Class") || (element.Type == "Component") || (element.Type == "Package"))
    {
         nodeList.Add(element);
    }
    Console.WriteLine("The nodes of MDG are:" + nodeList); //stampato a schermo la lista dei nodi nel MDG finale

我得到的输出是:

The nodes of MDG are:System.Collections.ArrayList

谁能告诉我为什么?

【问题讨论】:

  • 你为什么使用数组列表而不是列表
  • 因为列表中的类型出现问题,然后当我在 foreach 循环中使用这些列表时。

标签: c# arraylist console.writeline


【解决方案1】:

nodeList 转换为字符串只会调用nodeList.ToString(),它会产生您看到的输出。相反,您必须遍历数组并打印每个单独的项目。

您也可以使用string.Join:

Console.WriteLine("The nodes of MDG are:" + string.Join(",", nodeList));

顺便说一句,没有理由(或借口)在 C# 2 及更高版本中仍然使用 ArrayList - 如果您不维护旧代码切换到 List<T>

【讨论】:

  • 我尝试了这种方法但不起作用,我为数组中的每个元素都得到了“System.__ComObject”。如何获取数组中的值而不是值的类型?
  • @Defi:在这种情况下,列表中的项目没有“用户友好”ToString() 实现。使用foreach 循环并写出您特别想要的属性。
【解决方案2】:

首先,没有充分的理由在 C# 中使用 ArrayList。您应该至少改用System.Collections.Generic.List<T>,即使在这里,它们也可能是更具体的可用数据结构。 从不使用像 ArrayList 这样的无类型集合。

其次,当您将对象传递给 Console.Writeline() 时,它只是调用对象的 .ToString() 方法。

ArrayList 不会覆盖从基础对象类型继承的 .ToString() 方法。

基本对象类型的 .ToString() 实现只是简单地打印出对象的类型。因此,您发布的行为正是预期的。

我不知道选择不为数组和其他序列类型覆盖 .ToString() 的原因,但简单的事实是,如果您希望它打印出数组中的各个项目,则必须编写用于迭代项目并自己打印它们的代码。

【讨论】:

    【解决方案3】:

    你必须遍历 arraylist 来获取它的值...

    foreach(var item in nodeList)
    {
        Console.WriteLine("The nodes of MDG are:" + item);
    }
    

    这会起作用..

    更新:

    使用元素代替节点列表

    Console.WriteLine("The nodes of MDG are:" + element);
    

    【讨论】:

    • 您想打印The nodes of MDG are: 以获得到达项目?
    • Console.WriteLine(arrayList1); 不起作用,这似乎是您的建议,您的奇怪更新。它只会打印类型。并且将 Console.WriteLine(arrayList1) 放在循环中更加荒谬,它只会重复显示类型。我想你的第一段代码是好的,但其余的没有意义。
    【解决方案4】:
    StringBuilder builder = new StringBuilder();
    foreach (EA.Element element in elementsCol)
    {
        if ((element.Type == "Class") || (element.Type == "Component") || (element.Type == "Package"))
        {
            builder.AppendLine(element.ToString());
    
        }
     }
     Console.WriteLine("The nodes of MDG are:" + builder.ToString());
    

    【讨论】:

      【解决方案5】:

      这会调用 nodeList.ToString()。对列表中的每个元素运行 ToString() 并将它们连接在一起会更有意义:

      Console.WriteLine("The nodes of MDG are:" + string.Join(", ", nodeList));
      

      【讨论】:

      • 不在 4.0 中。 4.0 有一个 IEnumerable 重载。 :-)
      • 啊,他们该这么做了。我需要更新 4.0
      • 当然。但要更正:有一个 IEnumerable 重载,而不是 IEnumerable。因此 ArrayList 将被视为上下文中的另一个对象,并且 string.Join(", ", nodeList) 的返回值将是“System.Collections.ArrayList”。我想毕竟需要 ToArray 。或者,最好切换到 List.
      • 这个string.Join(", ", nodeList) 不起作用,它只显示System.Collections.ArrayList
      • 使用此方法仍然显示错误的输出
      【解决方案6】:

      我通过以下代码得到了我想要的输出:

      using System.IO
      
      using (StreamWriter writer = new StreamWriter("C:\\out.txt"))
              {
                  Console.SetOut(writer);
               }
      
      Console.WriteLine("the components are:");
              foreach (String compName in componentsList)
              { Console.WriteLine(compName); }
      

      componentsList 是我想要打印的数组列表。

      谢谢大家的帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-28
        • 2017-08-11
        • 1970-01-01
        • 2016-06-19
        • 2011-02-09
        • 1970-01-01
        • 1970-01-01
        • 2020-09-25
        相关资源
        最近更新 更多