Conversion of System.Array to List
Save yourself some pain...
using System.Linq;
int[] ints = new [] { 10, 20, 10, 34, 113 };
List<int> lst = ints.OfType<int>().ToList(); // this isn't going to be fast.
Can also just...
List<int> lst = new List<int> { 10, 20, 10, 34, 113 };
or...
List<int> lst = new List<int>();
lst.Add(10);
lst.Add(20);
lst.Add(10);
lst.Add(34);
lst.Add(113);
or...
List<int> lst = new List<int>(new int[] { 10, 20, 10, 34, 113 });
or...
var lst = new List<int>();
lst.AddRange(new int[] { 10, 20, 10, 34, 113 });
How do I convert an Array to a List object in C#?
List<object> list = myArray.Cast<Object>().ToList();
If the type of the array elements is a reference type, you can leave out the .Cast<object>()
since C#4 added interface co-variance i.e. an IEnumerable<SomeClass>
can be treated as an IEnumerable<object>
.
List<object> list = myArray.ToList<object>();
Conversion from System.Array to List string using LINQ, need to keep empty values
try this:
xmlData.AddRange(myvals.Cast<object>().Select(O => O == null ?
string.Empty :
O.ToString()).ToList());
Convert array of strings to List string
Just use this constructor of List<T>
. It accepts any IEnumerable<T>
as an argument.
string[] arr = ...
List<string> list = new List<string>(arr);
Converting array to list in Java
In your example, it is because you can't have a List of a primitive type. In other words, List<int>
is not possible.
You can, however, have a List<Integer>
using the Integer
class that wraps the int
primitive. Convert your array to a List
with the Arrays.asList
utility method.
Integer[] numbers = new Integer[] { 1, 2, 3 };
List<Integer> list = Arrays.asList(numbers);
See this code run live at IdeOne.com.
how to convert an array of objects to list
Just pass the array in List constructor.
IList<PersonData> aPerList = new List<PersonData>(aPersonDataArr);
To Convert back:
PersonData[] array = aPerList.ToArray();
If you don't want to use LINQ ToArray
then Use List.CopyTo:
PersonData[] newArray = new PersonData[aPerList.Count];
aPerList.CopyTo(arrArray,0);
convert List Object to System.Array[]
Use a little bit more LINQ
Select
will help you do the trick:
Array[] Values = val.Select(x => (Array)x).ToArray();
Note: every x
must be convertible to Array
.
That said, Array
itself is already an array. Array[]
seems to be Array of Arrays
to me and the data types of the elements are not strongly typed.
Array a = new int[10]; //this is ok
Array b = new string[10]; //this is also ok
Thus, your Array[]
may contain any strongly-typed arrays. {int[], string[], double[]}
and so on...
Unless it is what you really want, it seems to be not so safe design - IMHO.
Convert array of Strings to list of Integers?
You only need to stream once.
Instead of using int Integer::parseInt(String s)
, you should use Integer Integer::valueOf(String s)
, so you don't have to call boxed()
or rely on auto-boxing.
Then use collect(Collectors.toList())
directly, instead of creating intermediate array first.
List<Integer> allAnswerList = Arrays.stream(allAnswers) // stream of String
.map(Integer::valueOf) // stream of Integer
.collect(Collectors.toList());
Array to List or List to Array, which conversion is faster?
Arrays.asList
is faster because it does not copy data - it returns an object wrapping the array passed to it, and implementing the List
interface. Collection.toArray()
copies the data to the array, so it runs in O(N) instead of O(1).
Related Topics
Call Ruby or Python API in C# .Net
Get All Inherited Classes of an Abstract Class
Why Are Unsigned Int's Not Cls Compliant
How to Get the Client's Ip Address in ASP.NET MVC
How to Hide Only the Close (X) Button
Getting Path Relative to the Current Working Directory
Why Some Types Do Not Have Literal Modifiers
Getting File Names Without Extensions
Check Whether Internet Connection Is Available with C#
Bundler Not Including .Min Files
Views in Separate Assemblies in ASP.NET MVC
Generic Constraints, Where T:Struct and Where T:Class
Parallel.Foreach VS Task.Run and Task.Whenall
Hosting External App in Wpf Window
Inheritance from Multiple Interfaces with the Same Method Name
C# Webbrowser Control - Form Submit Not Working Using Invokemember("Click")