How to convert List<string[]> into List<int[] [duplicate] - c#

This question already has an answer here:
how to convert double array into integer in C#?
(1 answer)
Closed 3 years ago.
Can I convert an List<string[]> into an List<int[]>?
Or do I have to convert the string array into int array and then put it into a new List like this?
List<string[]> arrayStringList = new List<string[]>();
List<int[]> arrayIntList = new List<int[]>();
foreach(string[] stringArray in arrayStringList)
{
int[] iArr = stringArray.Select(int.Parse).ToArray();
arrayIntList.Add(iArr);
}

In one line
arrayIntList = arrayStringList.Select(x => x.Select(int.Parse).ToArray()).ToList();

Related

Flatten jagged array and map indexes [duplicate]

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);

Assign a GUID in C# [duplicate]

This question already has answers here:
All possible array initialization syntaxes
(19 answers)
Closed 2 years ago.
I have a string representation of the Guid,I'm doing some testing and I'm hard-coding some GUIDs into the code. The below way can assign Guid of single value.
Guid g = new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00");
Now, My question is how i can assign values to an array list of Guid[]?
You can use the below code snippet to assign values to an array list of Guid[]
Guid[] arr = new Guid[4];
arr.SetValue(new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"), 0);
arr.SetValue(new Guid("26a285e5-5435-4daa-8d36-3186bd441cff"), 1);
arr.SetValue(new Guid("6d850084-d55a-45a9-9e83-5adf22cfbf04"), 2);
arr.SetValue(new Guid("ec43e526-51c0-41bc-b04a-2f2e72c894cc"), 3);
You can use the below code snippet to fill out your Guid array with randomly generated Guids.
Guid[] arr = new Guid[10];
for (int i = 0; i < 10; i++)
{
arr.SetValue(Guid.NewGuid(), i);
}

How to obtain an integer array from a string [duplicate]

This question already has answers here:
Read numbers from the console given in a single line, separated by a space
(7 answers)
Split string, convert ToList<int>() in one line
(11 answers)
Closed 3 years ago.
I have gone through this solution. But this is not solving my problem. Let's say I have a string:
var aString = "0 -1 12 456 -512";
I want to convert this string to an int array like:
var convertedArray = [0, -1, 12, 456, -512];
How should I approach to solve this problem?
You can simply do this:
var stringNumbers = aString.Split(' ');
var numbers = new int[stringNumbers.Length];
for (int i = 0; i < stringNumbers.Length; i++)
numbers[i] = Convert.ToInt32(stringNumbers[i]);
var convertedArray = Array.ConvertAll(aString.Split(' '), int.Parse);

How to replace all items in an array of doubles with the same value [duplicate]

This question already has answers here:
How to populate/instantiate a C# array with a single value?
(26 answers)
Closed 6 years ago.
So lets say we have an array of doubles like this one that is later used for other stuff.
double[] myArray = new double[25];
How would I go about replacing all the values in that array with a set value?
There are flashier ways, but
for (int i = 0; i < myArray.Length; ++i){
myArray[i] = foo; /*the new value*/
}
is clear and simple.
The "flashy" way (simply create a new array):
var array = Enumerable.Repeat(value, count).ToArray();
Or
Array.ConvertAll(array, e => value);

Randomly generate specific strings in C# [duplicate]

This question already has answers here:
How to access random item in list?
(12 answers)
Closed 7 years ago.
This is the first time I'm writing a C# code.
In my C# code, I need to generate a string that can be any of these:
"00000041", "0000424E", "00004244", "00004D53"
How can you do this? How can you specify strings and randomly generate anyone from them?
this selects randomly one string out of the list of predefined strings
Random rnd= new Random();
List<string> validStrings= new List<string>() {
"00000041",
"0000424E",
"00004244",
"00004D53" };
string result = validStrings[rnd.Next(0, validStrings.Count)];
string[] s1 = new string[4] { "00000041", "0000424E", "00004244", "00004D53" };
Random rnd = new Random();
int randIndex = rnd.Next(0,4);
var randomString = s1[randIndex];

Categories