convert part of a list to an array
-
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
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
-
you're too generous. :-D
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
you're really too generous. :-D
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Best answer! :thumbsup:
The difficult we do right away... ...the impossible takes slightly longer.
so far, yes. :laugh:
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
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
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 -
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 KreskowiakI'm sure you'll find a loop inside, as well as inside
List.ToArray()
andArray.Copy()
. And even ifList.GetRange()
were returning anIEnumerable
rather than a list, there still would be a loop doing the hard work. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
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
-
you're too generous. :-D
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
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
Use ArrayList. example: ArrayList AryObj= new ArrayList(); for (int i=0; i < selec.Item.Count; i++) { AryObj.Add(selec.Item[i].Text); }
-
IMO you're right! :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Use ArrayList. example: ArrayList AryObj= new ArrayList(); for (int i=0; i < selec.Item.Count; i++) { AryObj.Add(selec.Item[i].Text); }
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 asList<int>
and there is no real use for ArrayList anymore, it is just an equivalent toList<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
-
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 KreskowiakI'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) -
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 asList<int>
and there is no real use for ArrayList anymore, it is just an equivalent toList<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
Thanks :)