Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. convert part of a list to an array

convert part of a list to an array

Scheduled Pinned Locked Moved C#
data-structurestutorial
21 Posts 8 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard Andrew x64R Richard Andrew x64

    Might I suggest a for loop:

    int Limit = selec.Count > 100 ? 100 : selec.Count;

    for ( int LoopCount = 0; LoopCount < Limit; LoopCount++ )
    copyselection[LoopCount] = selec[LoopCount];

    The difficult we do right away... ...the impossible takes slightly longer.

    V Offline
    V Offline
    VJ Reddy
    wrote on last edited by
    #6

    Good answer. 5!

    L 1 Reply Last reply
    0
    • V VJ Reddy

      I think the Take extension method of IEnumerable can be used for this purpose as shown below:

      List<int> data = new List<int>(){1,2,3,4,5,6,7,8,9,10};
      int[] array = data.Take(5).ToArray();

      The Take method takes specified number of elements or the total elements of the list if the total is less than the specified number.

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #7

      Best answer! :thumbsup:

      The difficult we do right away... ...the impossible takes slightly longer.

      L 1 Reply Last reply
      0
      • A a fatemeh

        Hi; I want to convert a list to an array but just part of it,because my array is smaller than the list.

        int[] copyselection = (int[])selec.ToArray(typeof(int[]));

        I want to limit the array elements for example ,I want to copy just 100 elements of it; Thank you all in advance

        T Offline
        T Offline
        taha bahraminezhad Jooneghani
        wrote on last edited by
        #8

        list items have a index, you can use for for selecting 100 item, or if you looking at some value in items use list.Where(p => p.StartsWith("s")) in a Foreach loop.some thing like this:

        List<string> lsi = new List<string>();
        lsi.Add("sara");
        lsi.Add("soo");
        lsi.Add("kabab");
        foreach (var item in lsi.Where(p => p.StartsWith("s")))
        {
        Response.Write(item);
        }

        1 Reply Last reply
        0
        • A a fatemeh

          Hi; I want to convert a list to an array but just part of it,because my array is smaller than the list.

          int[] copyselection = (int[])selec.ToArray(typeof(int[]));

          I want to limit the array elements for example ,I want to copy just 100 elements of it; Thank you all in advance

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #9

          With all the upvoted answers so far, you haven't gotten the proper solution yet IMO; I recommend

          int[] mySmallArray=myList.GetRange(startIndex, count).ToArray();

          which avoids both a big intermediate array and any visible looping, and doesn't require .NET version 3.5+ :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          D V 2 Replies Last reply
          0
          • V VJ Reddy

            Good answer. 5!

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #10

            you're too generous. :-D

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            V 1 Reply Last reply
            0
            • V VJ Reddy

              Good answer. 5!

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #11

              you're really too generous. :-D

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              1 Reply Last reply
              0
              • Richard Andrew x64R Richard Andrew x64

                Best answer! :thumbsup:

                The difficult we do right away... ...the impossible takes slightly longer.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #12

                so far, yes. :laugh:

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                1 Reply Last reply
                0
                • L Luc Pattyn

                  With all the upvoted answers so far, you haven't gotten the proper solution yet IMO; I recommend

                  int[] mySmallArray=myList.GetRange(startIndex, count).ToArray();

                  which avoids both a big intermediate array and any visible looping, and doesn't require .NET version 3.5+ :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #13

                  You sir, are too modest.

                  Luc Pattyn wrote:

                  and any visible looping

                  Way to cover your ass! ;) Curiously, I'll have to dig into the GetRange code now...

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  L D 2 Replies Last reply
                  0
                  • D Dave Kreskowiak

                    You sir, are too modest.

                    Luc Pattyn wrote:

                    and any visible looping

                    Way to cover your ass! ;) Curiously, I'll have to dig into the GetRange code now...

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #14

                    I'm sure you'll find a loop inside, as well as inside List.ToArray() and Array.Copy(). And even if List.GetRange() were returning an IEnumerable rather than a list, there still would be a loop doing the hard work. :)

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      With all the upvoted answers so far, you haven't gotten the proper solution yet IMO; I recommend

                      int[] mySmallArray=myList.GetRange(startIndex, count).ToArray();

                      which avoids both a big intermediate array and any visible looping, and doesn't require .NET version 3.5+ :)

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                      V Offline
                      V Offline
                      VJ Reddy
                      wrote on last edited by
                      #15

                      Really good answer. 5!

                      L 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        you're too generous. :-D

                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                        V Offline
                        V Offline
                        VJ Reddy
                        wrote on last edited by
                        #16

                        Thank you :-D

                        1 Reply Last reply
                        0
                        • A a fatemeh

                          Hi; I want to convert a list to an array but just part of it,because my array is smaller than the list.

                          int[] copyselection = (int[])selec.ToArray(typeof(int[]));

                          I want to limit the array elements for example ,I want to copy just 100 elements of it; Thank you all in advance

                          A Offline
                          A Offline
                          Apocalypse Now
                          wrote on last edited by
                          #17

                          Use ArrayList. example: ArrayList AryObj= new ArrayList(); for (int i=0; i < selec.Item.Count; i++) { AryObj.Add(selec.Item[i].Text); }

                          L 1 Reply Last reply
                          0
                          • V VJ Reddy

                            Really good answer. 5!

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #18

                            IMO you're right! :)

                            Luc Pattyn [My Articles] Nil Volentibus Arduum

                            1 Reply Last reply
                            0
                            • A Apocalypse Now

                              Use ArrayList. example: ArrayList AryObj= new ArrayList(); for (int i=0; i < selec.Item.Count; i++) { AryObj.Add(selec.Item[i].Text); }

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #19

                              That is bad. ArrayList is the only list that existed back in .NET 1.0 and 1.1; since 2.0 we have generic lists such as List<int> and there is no real use for ArrayList anymore, it is just an equivalent to List<object> and therefore it is superfluous as a type. Furthermore it does not contribute at all to the solution of the OP's problem. :|

                              Luc Pattyn [My Articles] Nil Volentibus Arduum

                              A 1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                You sir, are too modest.

                                Luc Pattyn wrote:

                                and any visible looping

                                Way to cover your ass! ;) Curiously, I'll have to dig into the GetRange code now...

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak

                                D Offline
                                D Offline
                                DaveyM69
                                wrote on last edited by
                                #20

                                I've just seen this and happened to have reflector open so I've had a look! GetRange uses Array.Copy to copy to a new list:

                                List list = new List(count);
                                Array.Copy(this._items, index, list._items, 0, count);
                                list._size = count;
                                return list;

                                as is ToArray but to an array instead:

                                T[] destinationArray = new T[this._size];
                                Array.Copy(this._items, 0, destinationArray, 0, this._size);
                                return destinationArray;

                                Now quite what Array.Copy is doing I'm not 100% sure as it's an extern function, so perhaps it's using unmanaged code to block copy?

                                Dave
                                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                                1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  That is bad. ArrayList is the only list that existed back in .NET 1.0 and 1.1; since 2.0 we have generic lists such as List<int> and there is no real use for ArrayList anymore, it is just an equivalent to List<object> and therefore it is superfluous as a type. Furthermore it does not contribute at all to the solution of the OP's problem. :|

                                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                                  A Offline
                                  A Offline
                                  Apocalypse Now
                                  wrote on last edited by
                                  #21

                                  Thanks :)

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups