c#计算数组

时间:2017-05-05 00:10:04

标签: c# arrays visual-studio overlap

我不是在寻找直接的答案,而是寻求某种类型的指导。如果您要发布直接代码,请您解释一下,因为我想尽可能多地学习。

我试图计算并输出两个数组之间的重叠。到目前为止,这是我的代码。

 class Tester
{
    static void Main(string[] args)
    {

        Box[] boxArray1 = {
                                new Box(4, 3, 2, "white"),
                                new Box(9, 5, 6, "red"),
                                new Box(3, 6, 12, "purple"),
                                new Box(15, 10, 4, "orange"),
                                new Box(4, 14, 10, "black"),
                          };

        Box[] boxArray2 = {
                                new Box(3, 4, 2, "pink"),
                                new Box(10, 2, 4, "red"),
                                new Box(8, 5, 7, "white"),
                                new Box(14, 4, 10, "blue"),
                                new Box(10, 15, 4, "bindle"),
                          };


    }//end of static main
    static void calculate1(Box[] box1) // <== Here's my attempt at calculating the overlaps.
    {
        string[] name = box1[i].Split(' ');
        int gr1 = box1.Length;
        int[] gg1 = new int[gr1];
        for (int i = 0; i < box1.Length; i++)
        {
            gg1[i] = int.Parse(box1[i]);
        }
    }

}//end of class Tester
class Container
{
    public string Colour { get; set; }
    public Container(string co)
    {
        Colour = co;
    }
    public virtual string GetContainerType()
    {
        return "Unknown";
    }
}//end of class Container

class Box : Container
{
    public double Length { get; set; }
    public double Height { get; set; }
    public double Width { get; set; }

    public Box(double Le, double He, double Wi, string co)
        : base(co)
    {
        Length = Le;
        Height = He;
        Width = Wi;
    }
    public override string GetContainerType()
    {
        return "Box";
    }
    public double GetVolume()
    {
        return Length * Height * Width;
    }


}//end of class Box

如上图所示,盒子的尺寸和颜色一致。我想找到这两个数组之间的重叠,例如&#34; White&#34;和&#34; Red&#34;,显示在两个数组中。然后我想要计算然后输出&#34;在两个数组之间有2个Box对象具有重叠的颜色。&#34;然后对Dimensions进行相同的操作。

感谢。

1 个答案:

答案 0 :(得分:0)

回答你问题中的最后一条评论,检查它是否会创建一个双循环并检查属性:

List<string> duplicatedColors = new List<string>();

for (int i = 0; i < boxArray1.Length; i++) //Loop through the first array
{
    for (int j= 0; j < boxArray2.Length; j++) //Loop through the second array
    {
        if(boxArray1[i].Color == boxArray2[j].Color) //Do element at array 1 have the same color that the item at array 2?
        {
            //Yes, store color and break inner loop
            duplicatedColors.Add(boxArray1[i].Color);
            break;
        }

        //No, continue loop
    }
} 

//Here now you have all the duplicated colors in duplicatedColors
//You can use duplicatedColors.Count to get the number of items dupicated.

好的,现在是卷:

List<double> duplicatedVolumes = new List<double>();

for (int i = 0; i < boxArray1.Length; i++) //Loop through the first array
{
    for (int j= 0; j < boxArray2.Length; j++) //Loop through the second array
    {

        var volumeA = boxArray1[i].Length * boxArray1[i].Width * boxArray1[i].Height; //Compute box A volume
        var volumeB = boxArray2[j].Length * boxArray2[j].Width * boxArray2[j].Height; //Compute box B volume

        if(volumeA - double.Epsilon < volumeB && volumeA + double.Epsilon > volumeB)
        {
          //Check if the volumes are equal, note the use of the Epsilon, that's because 
          //double values aren't exact and we must have that into account. double.Epsilon 
          //is the smallest (not lowest) value a double can store so it's very 
          //thightened, you can increase this value if you have errors.

            duplicatedVolumes.Add(volumeA);
            break;
        }

        //No, continue loop
    }
}