【问题标题】:Question about handling of ArrayList with Objects关于用对象处理 ArrayList 的问题
【发布时间】:2019-12-27 16:46:58
【问题描述】:

我想使用来自另一个类的动态数字 ob 对象作为我的类“公司”中的属性。为此,我想用 ArrayList 来处理这个问题。以下哪种方式有效或更好?还是有其他需要改进的想法?

public ArrayList IndustryList = new ArrayList();


//Method return an array of Class IndustryID
IndustryID[] industry = IndustryID.get_IndustryID();

//Did both options work or which is better?
//Option1
IndustryList.Add(industry);

//Option2
foreach (IndustryID industry_obj in industry)
  {
   Industrylist.Add(industry_obj);
  }

【问题讨论】:

  • 您使用什么语言?有些语言可以让您向现有列表添加范围,例如 C# 中的 List.AddRange。
  • 我正在为此使用 C#
  • 然后你可以使用ArrayList.AddRange()。在此处查看文档:docs.microsoft.com/en-us/dotnet/api/…

标签: c# object arraylist


【解决方案1】:

首先从IndustryID.get_IndustryID();返回此类型List<IndustryID>
然后;

public class Company{
    public List<IndustryID> Industries; 
    Company(List<IndustryID> industries){
        Industries = industries;
    }
}

用法:

 Company obj = new Company(IndustryID.get_IndustryID());
 foreach(var one in obj.Industries){
     Console.WriteLine(one.Name);
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 2011-02-15
    相关资源
    最近更新 更多