Array to ArrayList

  • Thread starter Thread starter Jerry S
  • Start date Start date
J

Jerry S

I've got a method that returns an array of Ints. What's the most
sensible way of copying them to a new ArrayList?

Thanks!

Jerry
 
Hi Jerry,
The ArrayList has a constructor that takes a collection as input.

int []items = {1,2,3,4,5};

ArrayList items2 = new ArrayList(items);



Chris
 
Hi

Thanks.

I thought I could do that, but its saying "Cannot implicitly convert
type 'int[]' to 'System.Collections.ArrayList'"

Any ideas where I might be going wrong?

Jerry


Hi Jerry,
The ArrayList has a constructor that takes a collection as input.

int []items = {1,2,3,4,5};

ArrayList items2 = new ArrayList(items);



Chris

Jerry S said:
I've got a method that returns an array of Ints. What's the most
sensible way of copying them to a new ArrayList?

Thanks!

Jerry
 
Hi

Ooops.

There was an extra "=" in my code.

Thanks.

It's now working!

Jerry

Hi Jerry,
The ArrayList has a constructor that takes a collection as input.

int []items = {1,2,3,4,5};

ArrayList items2 = new ArrayList(items);



Chris

Jerry S said:
I've got a method that returns an array of Ints. What's the most
sensible way of copying them to a new ArrayList?

Thanks!

Jerry
 
...
I've got a method that returns an array of Ints.
What's the most sensible way of copying them to
a new ArrayList?

First you should consider if you really need to make this copying, since an
array in itself implements the interfaces ICollection, IList and
IEnumerable.

If you then decide that you want to do this operation anyway, there are many
ways to do it, depending on what you mean by "copying".

int[] arrayOfInts;

// Some initializing and population of the array...

ArrayList myList = new ArrayList(arrayOfInts);

....is possibly the simplest way.

// Bjorn A
 
Back
Top