【问题标题】:Class with array where keys are strings and takes integers, doubles and strings带有数组的类,其中键是字符串并接受整数、双精度和字符串
【发布时间】:2013-10-10 09:47:53
【问题描述】:

我正在学习 C# 在线自学课程,但有一个作业我想不通。 我得到了一个半准备好的应用程序,我需要创建一个类和所有成员才能让它工作。我无法更改“类应用程序”下的任何内容。这是给我的模板:

using System;

namespace ObjectOriented
{
// Your Code Here

class Application
{
    static void Main()
    {
        TypeCollection collection = new TypeCollection(3);
        collection["integer"] = 123;
        collection["double"] = 456.78;
        collection["string"] = "Hello world!";

        double sum = (double)(int)collection["integer"] + (double)collection["double"];
        Console.WriteLine("The sum of all numbers is {0}", sum);

        Console.WriteLine();

        TypeCollection collection2 = new TypeCollection(2);
        collection2["integer"] = 123;
        collection2["double"] = 456.78;
        collection2["string"] = "Hello world!";
    }
}
}

这是它应该打印的内容:

 Added integer = 123
 Added double = 456.78
 Added string = Hello world!
 The sum of all numbers is 579.78

 Added integer = 123
 Added double = 456.78
 Collection is full

我最大的问题是如何创建带有键作为字符串的数组。这是我尝试过的,但我无法让它接受字符串作为键。

public class TypeCollection
{
    private object[] colType;

    public TypeCollection(object length)
    {
        this.tyyppi = new object[(int) length];
    }
    public object this[string key]
    {
        get
        {
            return colType[key];
        }
        set
        {
            colType[key] = value;
        }
    }
}

【问题讨论】:

  • 使用Dictionary<string, object> 而不是object[]
  • 您的集合对象不是数组,它 - 正如名称已经适用的那样 - 一个集合。这表示您还可以使用键访问所有集合条目,就像它是一个数组一样,但也可以使用字符串作为键,而不仅仅是索引。
  • 创建通用集合,一个用于字符串,一个用于双精度,一个用于整数。

标签: c#


【解决方案1】:

由于这是一个作业,我将尝试提供一些提示,而不是给出完整的答案。在这种情况下,[] 运算符不仅可以用于 C# 中的数组。该类也称为 TypeCollection,因此您应该查看 C# 中的一些集合文档以找到满足您需求的集合。

http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx

【讨论】:

  • 我同意这种方法——你经常在挖掘其他东西的同时发现很多有趣的东西——很遗憾它被其他用户的评论毁了!
  • 这种方法很适合我。在阅读我的问题的评论之前找到了字典。但是我正在学习的课程并没有提到任何关于收藏的内容。所有的理论都是关于类和数组的。所以我想知道,有没有办法做到这一点,而不使用字典?
  • 还有一些其他的集合可以使用,但如果你的意思是这是否可以用 C# 中的数组来完成,那么答案是“不”。 .NET 中的数组编译为 System.Array (msdn.microsoft.com/en-us/library/system.array.aspx)。在 .NET 中可以用数组完成一些有趣的事情,但索引始终是一个 int(例如,msdn.microsoft.com/en-us/library/eha9t187.aspx)。我希望这会有所帮助! :)
【解决方案2】:

使用Hashtable。它给了你想要的。请参阅下面的代码。

static void Main(string[] args)
    {
        Hashtable collection = new Hashtable();

        collection["integer"] = 123;
        collection["double"] = 456.78;
        collection["string"] = "Hello world!";

        double sum = (double)(int)collection["integer"] + (double)collection["double"];
        Console.WriteLine("The sum of all numbers is {0}", sum);

        Console.WriteLine();
    }

【讨论】:

    【解决方案3】:

    如果你想要只使用数组和类的东西,你必须使用这样的东西:

    public class TypeCollection
    {
        class KeyValue // define your key / value object to store the data
        {
            public string Key
            {
                get;
                set;
            }
    
            public object Value
            {
                get;
                set;
            }
        }
    
        private KeyValue[] colType;
    
        public TypeCollection(object length) // why use object instead of int???
        {
            this.colType = new KeyValue[(int)length];
        }
        public object this[string key]
        {
            get
            {
                for (var i = 0; i < colType.Length; i++) // find the key inside the array
                {
                    if (colType[i].Key == key)
                    {
                        return colType[i].Value;
                    }
                }
    
                return null;
            }
            set
            {
                // this should add new elements so you have to resize the array etc.
                for (var i = 0; i < colType.Length; i++)
                {
                    if (colType[i].Key == key)
                    {
                        colType[i].Value = value;
    
                        return;
                    }
                }
            }
        }
    }
    

    以此为起点。

    【讨论】:

      猜你喜欢
      • 2016-08-19
      • 2013-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多