473,396 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

List -> Array

Hello,

What's the easiest way to convert List -> Array in c#? Say for example the
same thing like in Java:

List list = new ArrayList();

Item[] items = (Item[]) list.toArray(new Item[list.size()]);

Is there a way to do this?

Thanks heaps,

/m
Nov 15 '05 #1
3 20859

"muscha" <mu****@no.spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello,

What's the easiest way to convert List -> Array in c#? Say for example the
same thing like in Java:

List list = new ArrayList();

Item[] items = (Item[]) list.toArray(new Item[list.size()]);

Pretty similar over all. If your list is an ArrayList, you can call
ArrayList.ToArray directly, if not, use the ArrayList(ICollection)
constructor:
//assumes you have an IList list defined.
//the new array list is used to provide ToArray, not pretty but ToArray
isn't provided in
//IList or ICollection
ArrayList arr = new ArrayList(list);
Item[] items = (Item[])arr.ToArray(typeof(Item));
Is there a way to do this?

Thanks heaps,

/m

Nov 15 '05 #2
muscha <mu****@no.spam.net> wrote:
What's the easiest way to convert List -> Array in c#? Say for example the
same thing like in Java:

List list = new ArrayList();

Item[] items = (Item[]) list.toArray(new Item[list.size()]);

Is there a way to do this?


Yes - and it's actually rather nicer in .NET/C#:

ArrayList list = new ArrayList();
....

Item[] items = (Item[]) list.ToArray (typeof(Item));

Note that it needs to be declared as ArrayList rather than IList, as
IList doesn't have the ToArray method, unfortunately.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
> > List list = new ArrayList();

Item[] items = (Item[]) list.toArray(new Item[list.size()]);

Is there a way to do this?


Yes - and it's actually rather nicer in .NET/C#:

ArrayList list = new ArrayList();
...

Item[] items = (Item[]) list.ToArray (typeof(Item));

Note that it needs to be declared as ArrayList rather than IList, as
IList doesn't have the ToArray method, unfortunately.


Ah that's why. I wonder why ToArray() is not defined in IList?

/m
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesnąt matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
0
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
0
by: saijin | last post by:
I'm planning to call a list of data from an XML file but when I duplicate the content inside the <data></data> it is not showing anything Here's the ActionScript 3.0 import...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.