Need help with an array in C#
I'm just starting to learn c# and I'm having trouble with arrays. 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 se开发者_JS百科t
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.
B1 -> 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 . . . Bn -> P1:M, P2:M, P3:M, P4:M, P5:MWhere "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. 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.
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)?
The box information will be int, the part information will be int, but the measurement will be double.
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[][]). 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.
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).
Take a look at the MSDN docs. 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.
Multidimensional Array MSDN Docs
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,}
};
Since what you are creating is always 3 wide, I saw in the C# wiki that the int[,]
might be what you want, but I don't know how to set it up.
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:
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]);
}
}
精彩评论