0

I am using C# and I have a List like this List<double[]>, I want convert into integer and store all elements in another list like this List<int[]>.

How can I do this?

5
  • 1
    Check it out... google.com/… You will want to try to Google and search existing SO answers before posting a question. Commented Sep 5, 2016 at 0:24
  • Possible duplicate of C# Converting List<int> to List<double> Commented Sep 5, 2016 at 0:25
  • @ShannonHolsinger it's the opposite I guess. Commented Sep 5, 2016 at 0:26
  • can you show us what you have tired..? have you tried a google search there are also look up the ConvertAll function Commented Sep 5, 2016 at 0:26
  • HAHAHAHAHA you got me on that one, @mok Commented Sep 5, 2016 at 0:27

1 Answer 1

1

There is a heap of ways to do this, for example you can use linq like this:

List<int> integers = new List<int>();
List<double> doubles = new List<double>();
for (int i = 0; i < 10; i++)
    doubles.Add(i + new Random().NextDouble());
foreach (var d in doubles)
{
    Console.WriteLine(d);
}
Console.WriteLine("------------------------");
integers = doubles.Select(d => (int) d).ToList(); // EVERYTHING IS DONE HERE
foreach (var i in integers)
{
    Console.WriteLine(i);
}

Or you can simply loop through the list or use an iterator and cast them explicitly.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @Mok. But, how can I convert string to List<double[]> or List<int[]>?
suppose you have a List of String called strings, you can do this: doubles = strings.Select(s => Double.Parse(s)).ToList();
Please @ Mok how can I implementation this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.