我如何创建数量可变的列表 [英] how can i creat variable number of lists

查看:59
本文介绍了我如何创建数量可变的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好大师,

我想问问是否可以创建可变数量的列表.

Hello masters,

I want to ask if it is possible to create variable number of lists.

public void CreateClusters(int NumberOfClusters)
       {
           for (int i = 1; i < NumberOfClusters ; i++)
           {
               List<Color> Cluster(i) = new List<Color>();
           }
       }


例如,当i为1时,我要创建一个名为"Cluster1"的颜色列表.
我是编程新手,感谢您的帮助.


For example when the i is 1, i want to create a color list named "Cluster1"

I am new at programming and thank you for your help.

推荐答案

这是两折的.基本上,您可以通过生成例如随后进行编译的代码来动态创建新的命名变量.但是,由于您提到您是编程新手,所以我会说这是错误的方法.您真的需要新的变量还是预定义类的新实例?

根据您给出的描述,我想您想在列表中分离Color的不同实例. 词典 [
This is two folded. Basically you can create new named variables on the fly by generating for example code that is then compiled. However, since you mentioned that you''re new to programming, I would say that this is a wrong approach. Do you really need new variables or just new instances of a predefined class?

Based on the description you have given I suppose you want to separate different instances of Color in your list. Would a Dictionary[^] satisfy your requirements?


只需使用一些简单的逻辑即可.从您的代码看来,您应该了解通用列表是所有内容的集合.那么,什么可以充当数量可变的列表的集合呢?毫不奇怪,列表列表:

Just use some simple logic. From your code, it looks like you should understand that a generic list is a collection of anything. So, what can act as a collection of variable number of lists? Not surprisingly, a list of lists:

using ColorList = System.Collections.Generic.List<Color>;
using ListOfColorLists = System.Collections.Generic.List<System.Collections.Generic.List<Color>>;

//...

public ListOfColorLists CreateClusters(int numberOfClusters) {
    ListOfColorLists list = new ListOfColorLists();
    for (int index = 0; index < numberOfClusters; ++index)
        list.Add(new ColorList());
    return list;
} //CreateClusters



请注意我做的一些固定的事情.首先,您尝试执行循环NumberOfClusters − 1次;您会错过一个元素,因此解决方法是从零索引开始.我还改进了您的命名,以满足Microsoft的命名约定.循环变量被重命名,因为一个字符的名称不好(例如,您将如何搜索?); ++index提供了比index++更好的性能,而结果没有变化. (这看起来很奇怪,但是请尝试对其进行测试.)将返回值添加到您的方法中,否则对象的创建将毫无意义,最终将导致垃圾回收器对其进行销毁(
http://en.wikipedia.org/wiki/Garbage_collector_%28computing%29 [



Please pay attention for some fixed I''ve made. First of all, you are trying to execute your loop NumberOfClusters − 1 times; you would miss one element, so the fix is to start with zero index. I also improved your naming to meet Microsoft naming conventions. Loop variable is renamed because one-character names are bad (how would you do search, for example?); ++index provides better performance then index++ without change in results. (It looks strange, but try to test it.) Return value is added to your method, otherwise creation of objects would be pointless and eventually would cause destruction of them by the Garbage Collector (http://en.wikipedia.org/wiki/Garbage_collector_%28computing%29[^]).

—SA


这篇关于我如何创建数量可变的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆