How can I convert ArrayList into string[] in C#?
string[] myArray = (string[])myarrayList.ToArray(typeof(string));
use .ToArray(Type)
string[] stringArray = (string[])arrayList.ToArray(typeof(string));
A simple Google or search on MSDN would have done it. Here:
ArrayList myAL = new ArrayList();
// Add stuff to the ArrayList.
String[] myArr = (String[]) myAL.ToArray( typeof( string ) );
Try do that with ToArray() method.
ArrayList a= new ArrayList(); //your ArrayList object
var array=(String[])a.ToArray(typeof(string)); // your array!!!
using System.Linq;
public static string[] Convert(this ArrayList items)
{
return items == null
? null
: items.Cast<object>()
.Select(x => x == null ? null : x.ToString())
.ToArray();
}
You can use CopyTo method of ArrayList object.
Let's say that we have an arraylist, which has String Type as Elements.
strArrayList.CopyTo(strArray)
Another way is as follows.
System.Collections.ArrayList al = new System.Collections.ArrayList();
al.Add("1");
al.Add("2");
al.Add("3");
string[] asArr = new string[al.Count];
al.CopyTo(asArr);
Related
I want to create a list of array type.
I want to create array containing values :
array = [a,b];
Then i want to put this array in list :
List<Array> list = new List<Array>( );
I am able to do this with list of string type but no luck with array type :
List<String> list = new List<String>( );
I am from javascript background, not much familiar with concept of collections in c#.
Also how can i create array in c# like we do in javascript :
var arrTemp = ["a", "b"];
Well, since your array is string[]:
var arrTemp = ["a", "b"];
you have to declare the required list as List<string[]>:
// list of string arrays
List<string[]> list = new List<string[]>() {
new string[] {"a", "b"}
};
In case you want to be able to put any array into the list declare it as loose as possible (List<object[]>):
// list of abitrary arrays
List<object[]> list = new List<object[]>() {
new string[] {"a", "b"},
new double[] {123.45, 789.12, 333.55},
new object[] {'a', "bcd", 1, 244.95, true},
};
Hope this can help you
var test = new List<int[]>();
You can actually create a list of arrays:
var listOfArrays = new List<Array>();
The problem with this is that it's difficult to use the arrays themselves, as the Array type doesn't support array syntax. (e.g. You can't do listOfArrays[0][0]) Instead, you have to use the GetValue method to do your retrieval:
var obj = listOfArrays[0].GetValue(0);
But this has another problem. The GetValue method returns object, so while you could always cast it to the desired type, you lose your type safety in choosing this approach.
Alternatively, you could just store object[] arrays:
var listOfArrays = new List<object[]>();
var obj = listOfArrays[0][0];
But while this solves the issue of the array notation, you still lose the type safety.
Instead, if at all possible, I would recommend finding a particular type, then just have arrays of that type:
var listOfArrays = new List<string[]>();
string s = listOfArrays[0][0];
for example, an array of strings would be
var arrayOfString = new string[]{"a","b"};
// or shorter form: string[] arrayOfString = {"a","b"};
// also: var arrayOfString = new[] { "a", "b" }
And then creating a list-of-arrayOfString would be
var listOfArrayOfString = new List<string[]>();
This works with any type, for example if you had a class MyClass
var arrayOfMyClass = new MyClass[]{ ... }; // ... is you creating instances of MyClass
var list = new List<MyClass[]>();
I'm completely new to c# sorry if asked here anything meaningless for you guys but I would like to know how can I solve this type of situation.
I having two arraylist's as shown below:
ArrayList OldLinks = new ArrayList();
ArrayList NewLinks = new ArrayList();
ArrayList mylist = new ArrayList();
foreach (string oldlink in OldLinkArray)
{
OldLinks.Add(oldlink);
}
foreach (string newlink in NewLinkArray)
{
NewLinks.Add(newlink);
}
Now I need to get them as single arraylist with two items each
I need to get it as
ArrayList NewList = new ArrayList();
NewList.Add(oldlink, newLink);
ArrayList NewList = new ArrayList();
NewList.AddRange(OldLinks);
NewList.AddRange(NewLinks);
You can use AddRange() method or AddAll() method to accomlish this.
NewList.AddAll(OldLinks);
NewList.AddAll(NewLinks);
Or
To create multidimensional arrayList you can use dictionary
public class MultiDimList: Dictionary<string, string> { }
MultiDimList NewList = new MultiDimList ();
for(int i; i<OldLinks.Count ; i++)
{
NewList.Add(OldLinks[i].ToString(), NewLinks[i].ToString());
}
provided both ArrayLists have the same count
Xou could do something like this.. The string Version is problaby not the best solution but can work. Sorry Code is note tested
public class Link
{
public string Version {get;set;}
public string Value {get;set;}
}
Use it Like
List<Link> linkList = new List<Link>();
linkList.AddRange(OldValues)
linkList.AddRange(OldValues)
var oldList = linkList.Where(l => l.Version.Equals("old")).ToList();
var newList = linkList.Where(l => l.Version.Equals("new")).ToList()
As you need both oldlink and newlink together as an item in resulted arraylist, you could use Zip Linq extension and do this.
ArrayList NewList = new ArrayList();
NewList.AddRange(OldLinks.Cast<string>()
.Zip(NewLink.Cast<string>(), (x,y) => string.Format("{0},{1}",x,y))
.ToArray()
);
Result ArrayList contains both (oldlink, newlink).
List<string> MyList = (List<string>)Session["MyList"];
MyList contains values like: 12 34 55 23.
I tried using the code below, however the values disappear.
string Something = Convert.ToString(MyList);
I also need each value to be separated with a comma (",").
How can I convert List<string> Mylist to string?
string Something = string.Join(",", MyList);
Try this code:
var list = new List<string> {"12", "13", "14"};
var result = string.Join(",", list);
Console.WriteLine(result);
The result is: "12,13,14"
Entirely alternatively you can use LINQ, and do as following:
string finalString = collection.Aggregate("", (current, s) => current + (s + ","));
However, for pure readability, I suggest using either the loop version, or the string.Join mechanism.
Or, if you're concerned about performance, you could use a loop,
var myList = new List<string> { "11", "22", "33" };
var myString = "";
var sb = new System.Text.StringBuilder();
foreach (string s in myList)
{
sb.Append(s).Append(",");
}
myString = sb.Remove(sb.Length - 1, 1).ToString(); // Removes last ","
This Benchmark shows that using the above loop is ~16% faster than String.Join() (averaged over 3 runs).
You can make an extension method for this, so it will be also more readable:
public static class GenericListExtensions
{
public static string ToString<T>(this IList<T> list)
{
return string.Join(",", list);
}
}
And then you can:
string Something = MyList.ToString<string>();
I had to add an extra bit over the accepted answer. Without it, Unity threw this error:
cannot convert `System.Collections.Generic.List<string>' expression to type `string[]'
The solution was to use .ToArray()
List<int> stringNums = new List<string>();
String.Join(",", stringNums.ToArray())
I have a string[] which contains value {"data1","data2","data3"}.
and i have a GenericList which contains
data2
data4
two records
i want to get the common datas which is avail in string[] and the genericList
Have you tried something like
string[] s = {"data1", "data2", "data3"};
List<string> list = new List<string> { "data2", "data3" };
var commonList = list.Intersect(s);
Have a look at Enumerable.Intersect Method (IEnumerable, IEnumerable)
Assuming it's a List<string> and you're using .NET 3.5 or higher, you can use the Intersect method from LINQ to Objects:
var intersection = stringArray.Intersect(stringList);
Note that this will return a lazily-evaluated IEnumerable<string>. If you need it in an array or a list, call the relevant method:
var intersectionArray = stringArray.Intersect(stringList).ToArray();
// or
var intersectionList = stringArray.Intersect(stringList).ToList();
Also note that this is a set operation - so the result will not contain any duplicates, even if there is duplication of a particular element in both the original collections.
Take a look at the Intersect extension method here
string[] c1 = { "data1", "data2", "data3" };
string[] c2 = { "data2", "data4" };
IEnumerable<string> both = c1.Intersect(c2);
foreach (string s in both) Console.WriteLine(s);
Will print data2.
if i have an array. can i populate a generic list from that array:
Foo[] fooList . . . (assume populated array)
// This doesn't seem to work
List<Foo> newList = new List<Foo>(fooList);
You could convert the array to a List:
string[] strings = { "hello", "world" };
IList<string> stringList = strings.ToList();
You are looking for List(t).AddRange Method
As #korki said, AddRange will work, but the code you've posted should work fine. For example, this compiles:
var i = new int[10];
var list = new List<int>(i);
Could you show us more of your code?