简体   繁体   English

在C#中需要帮助的数组

[英]Need help with an array in C#

I'm just starting to learn c# and I'm having trouble with arrays. 我刚刚开始学习c#,但数组遇到了麻烦。 The array I want will be multidimensional, but I'm not sure of the proper way to set it up. 我想要的数组将是多维的,但是我不确定设置它的正确方法。 There will be a total of three dimensions in this array and if it were printed out it would look similar to this: 此数组中总共有三个维度,如果打印出来,它将看起来类似于以下内容:

1, 23, 6.4
1, 29, 1.0
1,  3, 8.68
1, 12, 0.001
1, -5, 0
2, 83, -5
2,  5, 14
2, 19, -12.5
2, 62, 8
2, 81, 1
3,  0, 1.4
3, 11, 1.7848
3, 55, 64.4
3, 82, 23
3,  6, 73.4

Dimension 1 will have a variable number of sets (would be pulled from a database) Dimension 2 will always have 5 sets Dimension 3 will always have 1 set 维度1将具有可变数量的集合(将从数据库中提取)维度2将始终具有5个集合维度3将始终具有1个集合

Any help would be appreciated. 任何帮助,将不胜感激。

[UPDATE] [更新]

I was in a hurry when I originally posted this so I didn't have time to explain fully what this will be used for. 当我最初发布此消息时,我很着急,所以我没有时间充分解释它的用途。 Here is some additional information... 这是一些其他信息...

Each day people sort through boxes of parts and take a measurement of the parts. 每天,人们对零件盒进行分类并测量零件。 Each box has 5 parts that need to be measured. 每个盒子有5个需要测量的部分。

B1 -> P1:M, P2:M, P3:M, P4:M, P5:M B1-> P1:M,P2:M,P3:M,P4:M,P5:M
B2 -> P1:M, P2:M, P3:M, P4:M, P5:M B2-> P1:M,P2:M,P3:M,P4:M,P5:M
B3 -> P1:M, P2:M, P3:M, P4:M, P5:M B3-> P1:M,P2:M,P3:M,P4:M,P5:M
.
.
.
Bn -> P1:M, P2:M, P3:M, P4:M, P5:M Bn-> P1:M,P2:M,P3:M,P4:M,P5:M

Where "B" is the information on the box (box serial_no), "P" is the information about each part (part_no), and "M" is the measurement of each part. 其中“ B”是框上的信息(框serial_no),“ P”是有关每个零件的信息(part_no),“ M”是每个零件的尺寸。 There will always be 5 parts in a box and we will take only one measurement on each part, but there will be a variable amount of boxes. 一个盒子里总是有5个零件,而我们每个零件只能进行一次测量,但是盒子的数量却是可变的。

Before I posted I had already done research on arrays ( [] ), multidimensional arrays ( [,] ), jagged arrays ( [][] ), lists and a combination of all of them. 在发布之前,我已经对数组([]),多维数组([,]),锯齿状数组([] []),列表及其所有组合进行了研究。 I know with arrays you have to specify the size of each dimension whereas with lists you can add more to them. 我知道使用数组必须指定每个维度的大小,而使用列表则可以为其添加更多内容。

Is there a way I could combine a 2D (which would hold the measurement information for each part) and a list (which would hold the list of boxes)? 有没有一种方法可以将2D(将保存每个零件的测量信息)和列表(将保存框的列表)组合在一起?

The box information will be int, the part information will be int, but the measurement will be double. 盒子信息将为int,零件信息将为int,但尺寸将为两倍。

Is this what you're looking for? 这是您要找的东西吗?

class Program
{
    public static void Main()
    {
        var hey = new int[][]
        {
            new int[]{11, 12, 13},
            new int[]{21, 22, 23},
            new int[]{31, 32, 33},
        };

        foreach (var row in hey)
        {
            foreach (int i in row)
            {
                Console.Write("{0} ", i);
            }

            Console.WriteLine();
        }

        Console.ReadLine();
    }
}

The idea is that you have an array of arrays. 这个想法是您有一个数组数组。 var hey = new int[][], means you're creating an "int array, array" (int[][]). var hey = new int [] [],表示您正在创建一个“ int array,array”(int [] [])。 When you want to access one of the elements you would write 当您要访问其中一个元素时,您将编写

var myElement = hey[x][y];

where x is the row, and y is the integer in the y column on that row. 其中x是该行,y是该行y列中的整数。

Hope this helps :) 希望这可以帮助 :)

Edit: 编辑:

I see now that you wanted multi-dimensional arrays. 我现在看到您想要多维数组。 I missed that to begin with. 首先我很想念它。 You've already posted a solution for that, but to make my answer complete, you can also instantiate multidimentional arrays like so (three-dimensional integer array): 您已经发布了一个解决方案,但是为了使答案更完整,您还可以像这样实例化多维数组(三维整数数组):

public static void Main()
{
    var hey = new string[,,] {
        {
            { "000", "001", "002" },
            { "010", "011", "012" },
            { "020", "021", "022" },
        },
        {
            { "100", "101", "102" },
            { "110", "111", "112" },
            { "120", "121", "122" },
        },
        {
            { "200", "201", "202" },
            { "210", "211", "212" },
            { "220", "221", "222" },
        },
    };

    for (int x = 0; x < 3; x++)
    {
        for (int y = 0; y < 3; y++)
        {
            for (int z = 0; z < 3; z++)
            {
                Console.WriteLine("{0}{1}{2} = {3}", x, y, z, hey[x, y, z]);
            }
            Console.WriteLine();
            Console.WriteLine();
        }

        Console.WriteLine();
    }

    Console.ReadLine();
}

The problem with using multi-dimensional arrays, is there is no concept of rows or columns within the array (only within the accessor). 使用多维数组的问题在于,数组中没有行或列的概念(仅在访问器中)。 So there is no built in way to get only the first row, or values in the first column, etc... This makes it difficult (if not impossible) to use with Linq (which is super fancy, but extremely useful). 因此,没有内置的方法只能获取第一行或第一列中的值,等等。这使得(如果不是不可能的话)很难与Linq一起使用(这是非常花哨的,但是非常有用)。

Take a look at the MSDN docs. 看一下MSDN文档。 MSDN documentation isn't always the best for examples but its a great place to start finding out how to implement something when you are first learning how to code in .NET and what certain properties/methods/functionality means. MSDN文档不一定总是最适合作为示例,但它是一个很好的地方,当您第一次学习如何在.NET中进行编码以及某些属性/方法/功能的含义时,便可以开始查找如何实现某些功能。

Multidimensional Array MSDN Docs 多维数组MSDN文档

  var multiDimArray = new object[]
   {
      new int[] {0, 1, 2, 3},
      new string[] {"A", "B", "C", "D"},
      new double[] {23.45, 0d, 18.9},
      new int[] {23, 45, 67, 23, 3433, 5656, 1,}
   };

由于您创建的内容总是3倍宽,因此我在C#Wiki中看到了int[,]可能就是您想要的,但是我不知道如何设置它。

This is what I ended up doing: 这就是我最终要做的事情:

List<PartMeasurements> N = new List<PartMeasurements>();

N.Add(new PartMeasurements(24, new double[,] { {54, -0.146}, {9, 1.2}, {16, 0.097}, {15, 1}, {64, -0.9774} }));
N.Add(new PartMeasurements(4, new double[,] { {32, 0.76}, {45, 1.472}, {18, 0.005}, {52, 1.1}, {31, -0.1} }));
N.Add(new PartMeasurements(73, new double[,] { {81, 1.56}, {24, 1.34}, {9, 0.2}, {2, 0.6}, {55, -0.5} }));

public class PartMeasurements
{
    public int BoxSerial_No;
    public double[,] partNumber_and_Measurement = new double[5, 1];

    public Numbers(int BoxSerial_No, double[,] partNumber_and_Measurement)
    {
        this.BoxSerial_No = BoxSerial_No;
        this.partNumber_and_Measurement = partNumber_and_Measurement;
    }
}

If I want to see what is in "N" I can do this: 如果要查看“ N”中的内容,可以执行以下操作:

foreach(var x in N)
{
     Console.Write("{0}\t", x.BoxSerial_No);
     for (int y = 0; y < x.partNumber_and_Measurement.GetLength(0); y++)
     {
        Console.Write("{0} ", x.partNumber_and_Measurement[y, 0]);
        Console.Write("{0} ", x.partNumber_and_Measurement[y, 1]);
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM