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
-
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
OK, then there are two methods of doing this. The first uses a smaller memory footprint as you would use a
for
loop and copy the data you want from each element in the List to the Array. The larger memory footprint version is a one-line method, but you'll end up converting your entire List to an array, then Array.Copy[^] out the section you want.A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
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
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.
-
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
I think the
Take
extension method ofIEnumerable
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. -
OK, then there are two methods of doing this. The first uses a smaller memory footprint as you would use a
for
loop and copy the data you want from each element in the List to the Array. The larger memory footprint version is a one-line method, but you'll end up converting your entire List to an array, then Array.Copy[^] out the section you want.A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
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.
-
I think the
Take
extension method ofIEnumerable
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.Best answer! :thumbsup:
The difficult we do right away... ...the impossible takes slightly longer.
-
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
list items have a index, you can use
for
for selecting 100 item, or if you looking at some value in items uselist.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);
} -
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)