I have populated my List<int> and it has 30 elements, but i'm having trouble in displaying first 7 elements of the list, can someone help me?
How to print limited range of the List<int>
Page 1 of 114 Replies - 242 Views - Last Post: 26 September 2012 - 01:48 PM
Replies To: How to print limited range of the List<int>
#2
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 08:01 AM
Really? You index it like an array.
List<Int32> fop = new List<int>();
fop.Add(1);
fop.Add(2);
Console.WriteLine(fop[0]);
Console.WriteLine(fop[1]);
#3
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 08:05 AM
Yeah, i'm sorry to ask this stupid question, but my brain just froze
and couldn't remember the answer.
Anyways thanks for the help
Anyways thanks for the help
#4
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 08:09 AM
Ha.. no prob.
#5
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 08:12 AM
Hope you don't mind this kinda questions
#6
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 08:52 AM
use linq and say
list.Take(7).ForEach(item => Console.WriteLine(item))
This post has been edited by ThrowsException: 26 September 2012 - 08:54 AM
#7
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 09:03 AM
linq? What is wrong with a normal for-loop
#8
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 10:08 AM
Well I don't know about you, but that linq looks a lot shorter to me.
#9
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 12:26 PM
Based on the level I'm assuming the OP is at (and I'm basing that assumption on the nature of the question), giving examples in for-loop format is probably more recognizable (and therefore more relevant to the OP) than linq. Shorter != more conducive to learning.
#10
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 12:39 PM
Julyuary, on 26 September 2012 - 05:08 PM, said:
Well I don't know about you, but that linq looks a lot shorter to me.
Because it is shorter........
Doesn't Take make a new List and iterate through the first 7 items of the original list to put them in then new one? And then you iterate over the items again to print them out
#11
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 01:12 PM
CasiOo, on 26 September 2012 - 02:39 PM, said:
Doesn't Take make a new List and...put them in then new one?
I believe this would only happen if you were to say
list.Take(7).ToList()
If you don't add the ToList you never actually materialize the data until you hit the ForEach() and start actually using the data.
Someone please correct me if I'm mistaken about this.
#12
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 01:23 PM
Ok, there's a lot of misinformation here.
First of all, the LINQ in question won't even compile. ForEach isn't defined in the LINQ libraries, and it isn't an extension method to IEnumerable<T> like the rest of the LINQ methods are.
.ForEach is an instance method defined on the List<T> class itself. It only works on Lists.
Since .Take returns an IEnumerable, this won't work. If one were to call .ToList on the result of .Take, then it would work, but that would be inefficient, because .ToList creates a new list from the IEnumerable it is called on.
No, Take does not make a new list. IEnumerables are lazily evaluated. Basically, each extension method adds to the instructions that can create the sequence, but are not actually evaluated until necessary. So, assuming that the ForEach would actually work on this (which we've already established it won't), .Take wouldn't create a new list, it would create an IEnumerable that knows how to get the data it needed. Then, in the ForEach, as each value was needed, it would be enumerated and used.
LINQ can be quite efficient, if you understand how it works. For something as simple as looping through a few values, it really doesn't matter how you do it. Simple is usually better than clever though.
First of all, the LINQ in question won't even compile. ForEach isn't defined in the LINQ libraries, and it isn't an extension method to IEnumerable<T> like the rest of the LINQ methods are.
.ForEach is an instance method defined on the List<T> class itself. It only works on Lists.
Since .Take returns an IEnumerable, this won't work. If one were to call .ToList on the result of .Take, then it would work, but that would be inefficient, because .ToList creates a new list from the IEnumerable it is called on.
Quote
Doesn't Take make a new List and iterate through the first 7 items of the original list to put them in then new one? And then you iterate over the items again to print them out
No, Take does not make a new list. IEnumerables are lazily evaluated. Basically, each extension method adds to the instructions that can create the sequence, but are not actually evaluated until necessary. So, assuming that the ForEach would actually work on this (which we've already established it won't), .Take wouldn't create a new list, it would create an IEnumerable that knows how to get the data it needed. Then, in the ForEach, as each value was needed, it would be enumerated and used.
LINQ can be quite efficient, if you understand how it works. For something as simple as looping through a few values, it really doesn't matter how you do it. Simple is usually better than clever though.
#13
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 01:35 PM
Which is why I still think that this is the best thing to do in this case:
for (var i = 0; i < 7; i++)
{
Console.WriteLine(myList[i]);
}
#14
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 01:39 PM
There really isn't a "best", since the topic in question is so simple. I mean, it's just a little list, it really doesn't matter how you go about it. For loop, foreach loop, LINQ query, it's all going to get ironed out by the JIT into something sub-millisecond anyway.
#15
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 01:48 PM
Sorry. I don't know why I had it in my head that ForEach was a linq extension. Absolutely right that you would have to call ToList() before my linq query would work.
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote










|