This question already has answers here:
Convert 2 dimensional array
(4 answers)
Closed 7 years ago.
Is there an elegant way to flatten a 2D array in C# (using Linq or not)?
E.g. suppose
var my2dArray = new int[][] {
new int[] {1,2,3},
new int[] {4,5,6}
};
I want to call something like
my2dArray.flatten()
which would yield
{1,2,3,4,5,6}
Any ideas?
You can use SelectMany
var flat = my2dArray.SelectMany(a => a).ToArray();
This will work with a jagged array like in your example, but not with a 2D array like
var my2dArray = new [,] { { 1, 2, 3 }, { 1, 2, 3 } };
But in that case you can iterate the values like this
foreach(var item in my2dArray)
Console.WriteLine(item);
Related
This question already has answers here:
Simplest way to form a union of two lists
(5 answers)
Closed last year.
hi please help me
string [] s = new string []{1 , 2 , 3 };
string [] e = new string [] {1 , 2 ,4};
the output what I want are :
1 2 3 4
You are declaring a string[], however your values are int. You must either use string values or change the type of your array to int.
Union of IEnumerable is the function you're looking for and it does everything for you.
int [] s = new int [] {1, 2, 3};
int [] e = new int [] {1, 2, 4};
int[] union = s.Union(e).ToArray();
This question already has answers here:
How to resize multidimensional (2D) array in C#?
(7 answers)
Closed 5 years ago.
I'm trying to use a multidimentional string array.
I can easily create it:
string[,] array = new string[,]
{
{"AA", "AB"},
{"AC", "AD"},
};
no issue here.
The idea is to add some more items after.
The assumption is that my array can be extended for many "rows" but just two "columns".
I've tried to do this:
array[0,0] = "AE";
array[0,1] = "AF";
but this does not seem to work. Why?
Also is there any way to add (concatenate/join?) e.g. in one go:
new string[,] { {"AE", "AF"} }
or
array[i,???] = {"AE", "AF"}
where i can be variable (e.g. to the last position) and the columns wouldn't have to be defined (based on 2 columns defined in the array)
this could add the new item into the existing array (at 3rd position)?
The string[,] is a very good solution to set up your items manually, but if you have external data that requires to be added to the array, I couldn't find any example it on any other posts.
Can someone help?
Thanks.
Regards,
you need a datastructure that can grow ( and shrink )
try :-
var array = new List<string[]> {new[] {"AE", "AF"}, new []{"A", "B"}};
and then
array.Add(new []{"XY", "AB"});
and you can access it like :-
Console.WriteLine(array[0][0]);
array[0][0] = "BB";
Console.WriteLine(array[0][0]);
If you like array initialization syntax, you can do that first and convert it to a list like :-
var initial = new[,] {{"H", "I"}, {"R", "V"}};
var array = Enumerable.Range(0, initial.GetUpperBound(0) + 1)
.Select(n => new[] {initial[n, 0], initial[n, 1]}).ToList();
This question already has answers here:
Compare two lists to search common items
(2 answers)
Closed 6 years ago.
I have two lists of integers, I want to compare them and save the same values in a new list.
So if I have:
List<int> list1 = new List<int>()
{
1,
2,
3
};
List<int> list2 = new List<int>()
{
2,
3,
4
};
I would want a new List<int> List3 that would contain numbers 2 and 3.
You can use the Linq Intersect methods which gives the common part:
List<int> a = new List<int>();
List<int> b = new List<int>();
var common = a.Intersect(b);
This question already has answers here:
Compare List and return matches in c#
(3 answers)
Closed 6 years ago.
I have two arrays
List<int> a
List<int> b
List<int> matches
And I need to put all matches in a third (match) array so that I can print that out...
I can print out both a and b like so.
a.Sort();
label1.Text = "";
foreach (int x in a)
label1.Text += x + " , ";
a.Clear();
And so on for "b"
but how to compare the two and only take the matching integers, put them in "matching" array and print them out the same way?
You could use a linq query to get values that are in both lists...
List<int> a = new List<int> {1,2,3};
List<int> b = new List<int> {2,4,6,3};
var matches = a.Intersect(b);
// Create comma-separated string of matching values...
string output = string.Join(",", matches);
This question already has answers here:
How can combine two enumerations with a custom function?
(4 answers)
Closed 9 years ago.
I have two string arrays
Array1 = {"Paul","John","Mary"}
Array2 = {"12","13","15"}
I would like to know whether it is possible to join these arrays so that the resultant arrays have something like
{"Paul12","John13","Mary15"}
var Array3 = Array1.Zip(Array2, (a, b) => a + b).ToList();
You can use a Zip.
var array1 = new[] {"Paul", "John", "Mary"};
var array2 = new[] {"12", "13", "15"};
var result = array1.Zip(array2, (a1, a2) => String.Concat(a1, a2)).ToArray();