I developing an image processing software.
int size = 3;
int[,] kernel = new int[size, size] {
{1, 2, 1},
{2, 4, 2},
{1, 2, 1}
};
When I compile my code, I have a compile error message "A constant value is expected" from size variable. I understand I can put 3 on my kernel array initialization or make my size constant. What I ask is the technical reason behind this error because this error don't make any sense for me.
You can either create an array with empty values by specifying only the size (which may be variable), or list the values in an initializer and optionally specify a constant size. But you can't combine an initializer with a non-constant size. In the case of the initializer you're allowed to specify constant values for the size, if you want to make sure that the initializer results in an array of a specific size.
Just get rid of the size parameters, your initializer list already specifies the size.
The compiler requires a constant expression for the array ranks. You can declare size as const int, or you can just let the compiler figure it out from the initialization expression:
int[,] kernel = new int[,] {
{1, 2, 1},
{2, 4, 2},
{1, 2, 1}
};
In the delclaration your are adding the elements, so you don't need to specify the size.
So this will work :
int[,] kernel = new int[,] {
{1, 2, 1},
{2, 4, 2},
{1, 2, 1}
};
Related
What I am trying to do is something like this (in c#):
int[,] arr2d = {
{0, 0},
{0, 0},
{0, 0}
};
This produces an array that is 2 columns wide and 3 rows in length with all values set to zero.
What I am trying to do is something such that i can initialize an array where I give a value x for width, and a value y for height, which will declare a 2d array of those specifications of which all values are zero.
Essentially, how do I make a 2d array where all values are zero of any height and width WITHOUT declaring each one individually as in the code above.
0 is a default int value, so you can do the same by this code
var arr2d = new int[3,2];
If you want to fill an Array other than the default value, you can use Fill,
Array.Fill(arrayToFill, value)
To reset filled array to default value (eg: in int array to reset to 0)
Array.Clear(arrayToReset, 0, arrayToReset.Length);
I'd like to pull a section of an array out for manipulation.
e.g. From an array that contains 50 items, i want to return items 12 to 22 in a new array
Im currently using LINQ which I am assuming is slow:
return fullArray.Skip(12).Take(22).ToArray();
Is there a quicker way?
The Array.Copy method is massively quicker than Linq (I've tested it before and it was 2 or 3 orders of magnitude quicker!)
var sourceArray = object[50];
var newArray = object[10];
// Copy 10 elements, starting at index 12, to newArray (starting at index 0)
Array.Copy(sourceArray, 12, newArray, 0, 10);
You can use Array.Copy Method (Array, Int32, Array, Int32, Int32) method;
Copies a range of elements from an Array starting at the specified
source index and pastes them to another Array starting at the
specified destination index. The length and the indexes are specified
as 32-bit integers.
For example;
int[] array1 = new int[50];
int[] array2 = new int[10];
Array.Copy(array1, 12, array2, array2.GetLowerBound(0), 10);
Aside from looping. Is there an efficient way to instantiate a 2D array from multiple 1D arrays.
My 1D arrays must behave as column vectors in my 2D rectangular array. So Column 0 = First 1D array, Column 1 = Second 1D Array, etc...
I've also tried System.Buffer.BlockCopy, but block copy will roll the single 1D array into multiple columns which is not what I want.
Simple example of how I want to transform:
var c1 = new int[] {1, 2, 3, 4};
var c2 = new int[] {5, 6, 7, 8};
var result = new int[,]
{{1,5},
{2,6},
{3,7},
{4,8}};
Looping with unsafe code and pointers will be the fasted approach that C# can offer. Use unsafe because it eliminates the bounds checking which cannot be eliminated in this case b the current JIT.
Loop sequentially through the 2D array in memory order when writing. This means that you need to write row-by-row instead of column-by-column. This trick optimizes memory access and allows you to just increment the write pointer instead of calculating the address each time (y*width+y).
Aside from looping this will do(since you did not specify this as also a no solution):
var c1 = new int[] { 1, 2, 3, 4 };
var c2 = new int[] { 5, 6, 7, 8 };
var c3 = new int[,]
{
{c1[0],c2[0]},
{c1[1],c2[1]},
{c1[2],c2[2]},
{c1[3],c2[3]}
};
So I couldn't seem to figure this out. In the following code:
int[] array1 = { 86, 66, 76, 92, 95, 88 };
int[] array2 = new int[6];
array2 = array1;
When array2 is "copying" the values of array1, is it creating new memory references or is it referencing the same memory index as the values in array1?
Arrays are reference types, therefore you are assigning the same reference.
Array types are reference types derived from the abstract base type
Array.
If you want to create a deep copy, you can use Array.Copy:
int[] array1 = { 86, 66, 76, 92, 95, 88 };
int[] array2 = new int[array1.Length];
Array.Copy(array1, array2, array1.Length);
Arrays are of reference type. You can easily check this yourself
array2[1] = 2;
Console.WriteLine(array1[1]); // will print out 2
When you change one you change the other because both point to (reference) the same memory location.
It is referencing the same array. So if you change a value in array1 it will also be changed in array2.
What kind of variable should I initialize and how do I do it?
For example
byte[][][] data = ?????;
The function is from LibJpeg.NET library. The function definition is
Int32 jpeg_read_raw_data (
Byte[][][] data,
Int32 max_lines
)
There is no good documentation for this function, so I don't know what to send it, but I want to try to send something and see what the function will return.
The problem is that I don't know how to call it.
byte[][][] is an array of (arrays of (arrays of bytes)), also called a jagged array. It's not the same as a three-dimensional array (e.g. byte[,,] x = new byte[10,5,3];).
Here is an example for allocating and initializing such a structure:
byte[][][] a = new byte[10][][];
for (int i = 0; i < 10; i++)
{
a[i] = new byte[5][];
for (int ii = 0; i < 5; i++)
{
a[i][ii] = new byte[3];
a[i][ii][0] = 42;
a[i][ii][1] = 52;
a[i][ii][2] = 62;
}
}
Oh, and by the way, here's a related question: C# 3 dimensional array definition issue.
And here's the MSDN entry on jagged arrays: http://msdn.microsoft.com/en-us/library/2s05feca.aspx.
byte[][][] data is a 3 dimensional array, allocate it accordingly
= {{{0,1,2}{3,4,5}{6,7,8}}
{{0,1,2}{3,4,5}{6,7,8}}
{{0,1,2}{3,4,5}{6,7,8}}}
NOTE! THIS WILL NOT WORK! This is a jagged array, so unless you define data as byte[3,3,3] then you are going to go nowhere.
It occurred to me: I have no idea why you are using this library. Is there a reason the built in .NET JPEG classes aren't sufficient? If so, I imagine there would be a way to use the LibJpeg.NET library to get a byte stream from a file and arrange it into a 3D array. This seems like a very "hacky" way to do this, and it makes me wonder if it's just a port of a C library with no updates to fit into a framework like .NET or even OOP.
After a short look into the sources of LibJpeg.NET it looks like you should initialize the first dimension of the data array with the number of scan lines:
byte[][][] data = new byte[max_lines][][];
The rest of the array is filled when reading the raw data.
I'm not too familiar with that API so I don't know how you would get the number of scan lines though.
Just to correct firoso's answer, you can allocate arrays like that, he just has not specified the types as required in C# (His code would work in Java, AFAIK)
private byte[][][] lol =
{
new[]
{
new byte[] {0, 1, 2},
new byte[] {3, 4, 5},
new byte[] {6, 7, 8}
},
new[]
{
new byte[] {0, 1, 2},
new byte[] {3, 4, 5},
new byte[] {6, 7, 8}
},
new[]
{
new byte[] {0, 1, 2},
new byte[] {3, 4, 5},
new byte[] {6, 7, 8}
}
};
The new[] is actually a shortcut for new byte[][] and the new byte[] cannot be shortened to new[] as then the compiler infers the numbers types as int.
I would not recommend using this method if you can use Heinzi's iterative method instead; I am only pointing out it is possible to allocate multidimensional arrays like this (it is good for a lot of specific data).