Is there something called array of list? - c#

I declared something like this
List<double> close = new List<double>();
I pass this list to a function XYZ and the function will fill it up with value. As I need to run this XYZ function many times, is there a way to create a array of list so that I can access the third list element 7 by typing listarray[2][6].

I think you need something like this:
List<List<double>> list = new List<List<double>>();
var list1 = new List<double>();
list1.Add(1);
list1.Add(2);
var list2 = new List<double>();
list2.Add(3);
list2.Add(4);
list.Add(list1);
list.Add(list2);
var element = list[1][1];
The value of element will be the element of the second list at index of 1.
In this case, 4.

Thats about it:
List<double>[] arrayOfLists = new List<double>[200];
arrayOfLists[0] = new List<double>();
arrayOfLists[0].Add(5);
Console.WriteLine(arrayOfLists[0][0]);

Yes, just make either an array of lists (if you want it fixed sized), or a list of lists.
List<List<double>> items = new List<List<double>>();
List<double> close = new List<double>()
items.Add(close); //close is now element 0 in the outer list.
close.Add(1.23);
double result = items[0][0]; //result now equals 1.23

Related

C# list of array type

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[]>();

Rearrange a list based on given order in c#

I have a list as follows:
{CT, MA, VA, NY}
I submit this list to a function and I get the optimum waypoint order list
{2,0,1,3}
Now I have to rearrange the list as per the order that is newly provided. i.e. after rearranging, the list should look like:
{VA, CT, MA, NY}
What is the optimum way to do it? Using linq is there a way?
You could try the following:
var list = new List<string>{"CT", "MA", "VA", "NY"};
var order = new List<int>{2, 0, 1, 3};
var result = order.Select(i => list[i]).ToList();
This seems like the simplest approach:
oldItems = LoadItems(); //{"CT","MA","VA","NY"};
List<string> newItems = List<string>();
foreach(int idx in returnedIndexes)
{
newItems.Add(oldItems[idx]);
}

Convert List<int>to List<List<int>>

Can i convert List<int>to List<List<int>> in c#?
When I use such construction
List<int>element=new List<int>();
List<List<int>>superElement= new List<List<int>>(element);
I get a fault,but can I do this in another way?
You can do it like this:
List<List<int>> superElement = new List<List<int>>();
superElement.Add(element);
List<int>element=new List<int>();
List<List<int>>superElement= new List<List<int>> { element };
That'll work. You can't pass the list in the constructor.
Try this :
var element = new List<int>();
var superElement = new List< List<int> >(){ element };
Yes. You can use the collection initializer
List<int> element = new List<int>();
List<List<int>> superElement = new List<List<int>> { element };
http://msdn.microsoft.com/en-gb/library/vstudio/bb384062.aspx
If you just want to initialize the List, then
List<List<int>>superElement= new List<List<int>>();
works; if you want it to contain one (empty) element initially then
List<List<int>>superElement= new List<List<int>>{element};
will do it.
Due to the fact I do not believe the other answers have really put in much effort to help, I am going to post what I believe to be a more complete and useful answer.
To start with, and for completeness I will show how to achieve what was actually trying to be done. To create a List of Lists you can do this:
List<int> element = new List<int>();
List<List<int>> superElement = new List<List<int>>();
superElement.Add(element);
But as it stands, it doesn't make a lot of sense why you would want to do this, and after some comment probing (hope it didn't hurt) we have discovered that you want to pair up an ID with a List of integers. I would suggest taking a different approach to this.
Personally, I would create a class to hold my data, and then create a single List for those items, like so:
public class MyData
{
public int ID {get; set;}
public List<int> MyValues {get;set;}
public MyData()
{
MyValues = new List<int>();
}
}
Then you can do this:
List<int> element = new List<int>();
MyData data = new MyData();
data.ID = 1;
data.MyValues = element;
List<MyData> superElement = new List<MyData>();
superElement.Add(data);
Which would allow querying like so:
MyData data1 = superElement.SingleOrDeafult(x => x.ID == 1);
List<int> element = data1.MyValues;
Assuming you have Linq available.
An alternate method could be to use a dictionary, like so:
Dictionary<int, List<int>> superElement = new Dictionary<int, List<int>>();
superElement.Add(1, element);
where 1 is an ID, which you can call like so:
List<int> element = superElement[1];

Compare the difference between two list<string>

I'am trying to check the difference between two List<string> in c#.
Example:
List<string> FirstList = new List<string>();
List<string> SecondList = new List<string>();
The FirstList is filled with the following values:
FirstList.Add("COM1");
FirstList.Add("COM2");
The SecondList is filled with the following values:
SecondList.Add("COM1");
SecondList.Add("COM2");
SecondList.Add("COM3");
Now I want to check if some values in the SecondList are equal to values in the FirstList.
If there are equal values like: COM1 and COM2, that are in both lists, then filter them from the list, and add the remaining values to another list.
So if I would create a new ThirdList, it will be filled with "COM3" only, because the other values are duplicates.
How can I create such a check?
Try to use Except LINQ extension method, which takes items only from the first list, that are not present in the second. Example is given below:
List<string> ThirdList = SecondList.Except(FirstList).ToList();
You can print the result using the following code:
Console.WriteLine(string.Join(Environment.NewLine, ThirdList));
Or
Debug.WriteLine(string.Join(Environment.NewLine, ThirdList));
Note: Don't forget to include: using System.Diagnostics;
prints:
COM3
You can use Enumerable.Intersect:
var inBoth = FirstList.Intersect(SecondList);
or to detect strings which are only in one of both lists, Enumerable.Except:
var inFirstOnly = FirstList.Except(SecondList);
var inSecondOnly = SecondList.Except(FirstList);
To get your ThirdList:
List<string> ThirdList = inSecondOnly.ToList();
Than for this king of reuqirement you can can make use of Except function.
List<string> newlist = List1.Except(List2).ToList();
or you can do this , so the below one create new list three which contains items that are not common in list1 and list2
var common = List1.Intersect(List2);
var list3 = List1.Except(common ).ToList();
list3.AddRange(List2.Except(common ).ToList());
the above one is help full when list1 and list2 has differenct item like
List<string> list1= new List<string>();
List<string> list2 = new List<string>();
The FirstList is filled with the following values:
list1.Add("COM1");
list1.Add("COM2");
list1.Add("COM4");
The SecondList is filled with the following values:
list2 .Add("COM1");
list2 .Add("COM2");
list2 .Add("COM3");
by using above code list3 contains COM4 and COM3.

Two ArrayList manipulation

I have one ArrayList in Session, lets say for example [305,306,380].
On submit, user choice other products which i save them in 2nd array, for example [390,305,480,380]
How can i make another three arrays where
All new values [390,480]
All values which are in both lists [305,380]
All values from list1 which are not in list2 [306]
I need this in ASP.NET 4.0 C#
You can use ArrayList.ToArray() to get arrays against your arraylists. Then using LINQ you can easily get what you want withExcept an Intersect methods, for example
array2.Except(array1)
array1.Except(array2)
array1.Intersect(array2)
Edit: Complete Code
According to your requirement, your code may look-like this;
ArrayList arrayList1 = new ArrayList(new int[] { 305, 306, 380 });
ArrayList arrayList2 = new ArrayList(new int[] { 390, 305, 480, 380 });
int[] array1 = (int[])arrayList1.ToArray(typeof(int));
int[] array2 = (int[])arrayList2.ToArray(typeof(int));
//1. All New values
int[] uniqueInArray2 = array2.Except(array1).ToArray();
//2. Common values
int[] commonValues = array1.Intersect(array2).ToArray();
//3. Values of arrayList1 which are not in arrayList2
int[] uniqueInArray1 = array1.Except(array2).ToArray();
Use HashSet the folowing way:
var first = new HashSet<int>();
first.Add(...);
var second = ...;
1. second.ExceptWith(first);
2. first.IntersectWith(second);
3. first.ExceptWith(second);

Categories