14 Replies - 447 Views - Last Post: 26 September 2012 - 01:48 PM Rate Topic: -----

#1 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,506
  • Joined: 08-April 09

How to print limited range of the List<int>

Posted 26 September 2012 - 07:54 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: How to print limited range of the List<int>

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8954
  • View blog
  • Posts: 33,569
  • Joined: 12-June 08

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

Was This Post Helpful? 2
  • +
  • -

#3 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,506
  • Joined: 08-April 09

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 :hammer: and couldn't remember the answer.
Anyways thanks for the help
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8954
  • View blog
  • Posts: 33,569
  • Joined: 12-June 08

Re: How to print limited range of the List<int>

Posted 26 September 2012 - 08:09 AM

Ha.. no prob.
Was This Post Helpful? 0
  • +
  • -

#5 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,506
  • Joined: 08-April 09

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 :)
Was This Post Helpful? 0
  • +
  • -

#6 ThrowsException  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 83
  • Joined: 21-February 12

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

Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1377
  • View blog
  • Posts: 3,035
  • Joined: 05-April 11

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
Was This Post Helpful? 0
  • +
  • -

#8 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 106
  • Joined: 20-December 11

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.
Was This Post Helpful? 0
  • +
  • -

#9 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

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.
Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1377
  • View blog
  • Posts: 3,035
  • Joined: 05-April 11

Re: How to print limited range of the List<int>

Posted 26 September 2012 - 12:39 PM

View PostJulyuary, 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
Was This Post Helpful? 0
  • +
  • -

#11 ThrowsException  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 83
  • Joined: 21-February 12

Re: How to print limited range of the List<int>

Posted 26 September 2012 - 01:12 PM

View PostCasiOo, 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.
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

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.

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.
Was This Post Helpful? 4
  • +
  • -

#13 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

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


Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

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.
Was This Post Helpful? 0
  • +
  • -

#15 ThrowsException  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 83
  • Joined: 21-February 12

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1