2015-11-03 27 views
0

我是C#的新手,已经阅读了C#编程的入门书,但我仍然不确定如何设置类的3D数组。这里是一个小背景:3维数组将被搜索

我需要跟踪堆栈中名为container的类的实例。将有n个堆栈需要被跟踪,因此是3d阵列。容器将被添加并从堆栈中移除。我将需要搜索所有容器以查找符合某些条件的容器。其中一个容器将被选中;该容器以及该特定堆栈中的所有容器将需要移动到另一个堆栈。

我想通过使用LINQ查询将是搜索所有容器的最佳方式,这意味着我应该使用某种类型的集合像List来容纳所有容器。我所见过的所有集合似乎都只有一个索引器,这让我觉得我可以只使用一个List并自己跟踪3D索引。

什么是正确的方法?

回答

0

你可以在C#代码中使用/管理这些比3D数组更好的!

var d3Stack = new Stack<Stack<Stack<MyJob>>>(); 
var d3List = new List<List<List<MyJob>>>(); 
0

请记住,C#可以将自己的对象构建为容器。这个概念通常被称为POCO,或简单的旧类对象。如果您想要制作更适合特定需求的集合,那么您可以根据需要进行更新,这将很容易创建这些对象。如果你想保持通用性而不是使用内置插件。代码应该在.NET 3.5中运行,并在Visual Studio中运行。我在LINQPad中创建了它。如果你想要更具体的功能,我会走这条路线,并且记住你可以随时改变你的对象。

所以我们来创建两个容器对象。一个是父母,另一个是该父母的孩子进行示范。在标准的控制台应用程序中,这些应用程序可能位于“主”入口点之外。然后,我将创建两种方法来做一些人群来展示使用POCO作为容器的要点。

public class JobListing 
{ 
    public int Id { get; set;} 
    public string Name { get; set; } 
    public List<Job> Jobs { get; set; } 
} 

public class Job 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Action { get; set; } 
} 

public List<Job> CreateJobs() 
{ 
    return new List<Job> 
    { 
     new Job { Id = 1, Name = "First", Action = "Does It"}, 
     new Job { Id = 2, Name = "Second", Action = "Does It Again"}, 
     new Job { Id = 3, Name = "Third", Action = "Does It Yet Again"} 
    }; 
} 

public List<JobListing> CreateJobListings() 
{ 
    return new List<JobListing> 
    { 
     new JobListing { Id = 1, Name = "First Area", Jobs = CreateJobs() }, 
     new JobListing { Id = 2, Name = "Second Area", Jobs = CreateJobs() } 
    }; 
} 

void Main() 
{ 
    // I am just creating a variable that evaluates at runtime to hold my demo data. 
    var jobslistings = CreateJobListings(); 

    // this is merely an example unboxing the data layer by layer 
    jobslistings.ForEach(x => { 
    //x => is a lambda statement and I am using Fluent Syntax off of a method that returns my containers. 
    //x is now each object in my collection. In this case it is a 'JobListing' object POCO I Made. 
     Console.WriteLine(string.Format("{0} {1}", x.Id, x.Name)); 
     Console.WriteLine("\tJobs"); 
     x.Jobs.ForEach(y => { 
     // y => is a lambda statement and I am now in an object of an object. 
      Console.WriteLine(string.Format("\t\t{0} {1}", y.Id, y.Name)); 
     }); 
    }); 

    Console.WriteLine(); 
    Console.WriteLine("Where Clause"); 
    Console.WriteLine(); 

    // now I am reusing my variable but with a 'predicate' lamba to do a where clause 
    // this may narrow down my lookups later and I could use a similar example 
    jobslistings.Where(predicate => predicate.Id == 1).ToList().ForEach(x => { 
    //x => is a lambda statement and I am using Fluent Syntax off of a method that returns my containers. 
    //x is now each object in my collection. In this case it is a 'JobListing' object POCO I Made. 
     Console.WriteLine(string.Format("{0} {1}", x.Id, x.Name)); 
     Console.WriteLine("\tJobs"); 
     x.Jobs.ForEach(y => { 
     // y => is a lambda statement and I am now in an object of an object. 
      Console.WriteLine(string.Format("\t\t{0} {1}", y.Id, y.Name)); 
     }); 
    }); 
}