How can i compare arrays in two jagged arrays C# - c#

Ok so i have 2 jagged arrays, i want to loop through them and compare each array to every array in the other jagged array.
So in this example i want to return true when access[1] == visual[0].
But it seems i cannot explicitly compare arrays in the jagged arrays. How can i reference the arrays as a whole and not only the elements within those arrays?
By this i mean if i write access[0][0] i will get "10". But i can't write access[0] to get "10","16"
string[][] access = new string[][] {
new string[] {"10","16"},
new string[] {"100","20"},
new string[] {"1010","2"},
new string[] {"1011","1"}
};
string[][] visual = new string[][] {
new string[] {"100","20"},
new string[] {"101","36"},
new string[] {"101","37"},
new string[] {"101","38"}
};

But i can't write access[0] to get "10","16"
You can. But to compare the elements you need to use Enumerable.SequenceEqual.
if (Enumerable.SequenceEqual(access[1], visual[0])) { ... }

Related

List to Jagged Array in C#

I have a list
List<int> list = new List<int>();
Now I want to add this to a Jagged Array
int[][] A = new int[][] { list.ToArray() };
This code on top is ok, but the problem is that all values in the list are add in the first block!
Well, then, this will be solved:
int[] x1 = list.ToArray();
int[][] A = new int[][] { new[] { x1[0] }, new[] { x1[1] }, new[] { x1[2] }, new[] { x1[3] } };
But (the code above) I've done this manually now, that's just the first four indexes I've of list put in the array..
How can I add entire list (all indexes) to my jagged array (with circles or other methods).
Use a projection.
This will iterate through the list, creating a new array with the sole value of the current iteration, and then end by creating an array of all of those arrays.
int[][] A = list.Select(i => new[] { i }).ToArray();
As an aside, and as [#maccettura] notes, this is a jagged array (where each member of the array is also an array itself).

c # - copying elements of one array to another - compile error

Learning some basics.. I'm trying to copy elements of an array to another. Let's say I don't know the size of the array 'bar'. So, I create an empty array 'arr' to copy the elements to bar into. The code below doesn't work.
It works if I replace
string[] arr ={} to string[] arr ={"",""}
How to declare an empty array and what should I modify in my code to achieve my goal?
Thanks!
//code
string[] bar = {"test", "user"};
string[] arr = {};
//iterate from the first to the last element of array bar
for (int i =0;i<bar.Length-1;i++)
{
Console.WriteLine("copy");
//copy string from bar to arr
arr[i]= bar[i];
//display the copied content from new array
Console.WriteLine(arr[i]);
}
in C#, arrays are of a fixed size. So when you create your array with size 0, you can't change the number of items it will contain, without re-instantiating it.
If you want to use a collection you can actively add/remove from (as is very common), consider using a List<T>:
string[] bar = {"test", "user"};
List<string> list = new List<string>();
for (int i =0;i<bar.Length-1;i++)
{
list.Add(bar[i]);
Console.WriteLine(list[i]);
}
By string[] arr = {}; you're instantiating an empty array of ZERO length, thus you need to define it like string[] arr = string[bar.Length];.
UPD:
Your code worked with string[] arr = {"",""}, because in this case you defined an array of length 2 using a two empty strings, but that's a code smell.
Arrays are fixed in size, which is why things like Lists are preferred over them.
In the case where you change your array definition to: string[] arr ={"",""} you are defining an array with a size of 2, same as your original array. When you try to copy it, the compiler already has everything allocated and ready to go, so it knows where position 0 and position 1 are in the array arr.
In the example in your code, where you have the array defined by string[] arr = {}; you are giving it an empty array (array size 0). The compiler has an issue, because it cannot reference position 0 or position 1 on an array that is empty.
You can modify the line as:
string[] arr = new string[4];
or
List<string> arr = new List<string>();
If you're going to use arrays, you'll want to create the second array as the same size as the first.
string[] bar = {"test", "user"};
string[] arr = new string[bar.Length];
If you know ahead of time that your array will be two, then you can just create it to be size two. Otherwise you'll want to inspect the size of the array you're copying from. If you know that you'll be adding and/or removing items, you'll want to use a different collection.

Passing dynamic arrays into the argument

I have a class constructor accepting params T[][] arrays.
public CartesianProduct(params T[][] arrays)
{
}
I am passing the arrays below which is working fine
string[] arr1 = { "MSG1" };
string[] arr2 = { "OFFER1", "OFFER2" };
string[] arr3 = { "CTA1", "CTA2" };
var cross = new CartesianProduct<string>(arr1,arr2,arr3);
This works fine if i know the number of arrays and then pass it in the argument. The problem is when i am creating a button in the windows form to add new arrays. For example i have a simple text box and a button which creates an array. Click Add new array will create another array. How can i pass these arrays in the argument? Help would be appreciated . Thanks in advance
You can not dynamically add a 4th parameter to the call. You can dynamically create an array containing an arbitrary number of other arrays and pass that:
var aList = new List<string[]>();
aList.Add(arr1);
//...
var cross = new CartesianProduct<string>(aList.ToArray());
You may consider adding another constructor that accepts a List directly.

difference between double[*,*] to double[][] in c#

i used
double [,] marks=new double[26,5]
int[] function = object.verify(marks)
public void verifymarks(double[][] marks)
error i get is cannot convert from double[,] to double[][]
i tried to search in the internet but couldnot find any solution. I have just began to use c#. Please Help. THankx in advance
double[][] is jagged. It's literally an array of arrays.
double[,] is multidimensional. It gets special compiler treatment.
They have different memory layouts. double[][] is an array which holds references to the other arrays, while double[,] is laid out single-dimensionally for easier use.
Another difference is what data they can hold. A jagged array can be like this:
1231243245345345345345
23423423423423
2342342343r234234234234234234
23423
While a multidimensional array has to be a perfect table, like this:
34534534534534
34534534534533
34534534534534
34534534534545
A way to get around it would be to use nullable ints.
Now, to solve your problem:
Change your method signature to public void verifymarks(double[,] marks) and in the method change anything that uses marks[x][y] to marks[x,y].
double[][] is called a Jagged array. It is an array of array (the same way you could create a list of list). With this structure you can specify a different size for each sub-array.
For exemple:
double[][] jaggedArray = new double[2][];
jaggedArray[0] = new double[5];
jaggedArray[1] = new double[151];
The other writing double[,] is a 2D array (Multidimensional array in general). You can see it as a table.
A very handy feature of multidimensional array is the initialization:
string[,] test = new string[3, 2] { { "one", "two" },
{ "three", "four" },
{ "five", "six" } };
Here's a visual, from LINQPad's Dump() function.
The first is the double[,], which creates a matrix. The second is double[][], which is just an array of arrays.

C# Need to pass an array of array's to another form

I want to pass an array of arrays to another Form
String[] arrayOfStrings = new String[4];
arrayOfStrings[0] = td1stcolumn[];
arrayOfStrings[1] = td2ndcolumn[];
arrayOfStrings[2] = td3rdcolumn[];
arrayOfStrings[3] = td4thcolumn[];
string resultDialogString = ResultDialog.ShowBox(arrayOfStrings, "Result Page");
td1stcolumn, td2ndcolumn, td3rdcolumn and td4thcolumn are all String arrays
C# is expecting values in the [] for td...column
Signature of ResultDialog.ShowBox is
public static string ShowBox(string[] arrayOfMessages, string txtTitle)
but I can modify the signature. I just need to transfer the data from the
td1stcolumn, td2ndcolumn, td3rdcolumn and td4thcolumn
to the ResultDialog.Show method
Assuming you have declared your "td" variables like this:
string[] td1stcolumn;
string[] td2ndcolumn;
string[] td3rdcolumn;
string[] td4thcolumn;
Then you can assign these values to your arrayOfStrings like this:
arrayOfStrings[0] = td1stcolumn;
arrayOfStrings[1] = td2ndcolumn;
arrayOfStrings[2] = td3rdcolumn;
arrayOfStrings[3] = td4thcolumn;
You'll also need to change the declaration for your string array to something like this:
// an array of arrays. Each array must be the same length.
// your situation calls for a jagged array though.
string[,] arrayOfStrings;
or
// jagged array. each array can be of varying length.
string[][] arrayOfSTrings;
Multi-Dimentional Array Documentation
Jagged Array Documentation
Example of a Jagged Array usage:
string[][] arr = new string[2][];
arr[0] = new [] {"a", "b", "c", "d"};
arr[1] = new [] {"a", "b", "c", "d"};
Seems like you might be better suited to a multidimension array...
// declare
String[,] multiArray = new String[4,4];
// use
string s = multiArray[2,2];

Categories

Resources