I've had a look at both ASP.Net c# adding items to jagged array and vb.net assign array to jagged array but I just can't seem to work this out...
So, essentially, it's this. I have a function that builds a list of values from a query or two. I'm adding each value returned to a list then converting the list to an array. All is good
However, I now need to return two values, so a multi dimension array appears more suitable. Essentially, this is what I want to do:
string[][] array2D = new string[2][];
array2D[0] = new string[3] { "one", "two", "three" };
array2D[1] = new string[3] { "abc", "def", "ghi" };
All good so far. However, I don't know the values that I want to plug in to the array at the time of array initialisation, so, this is what I'd expect to be able to do:
string[][] array2D = new string[2][];
//array2D[0] = new string[3] { "one", "two", "three" };
//array2D[1] = new string[3] { "abc", "def", "ghi" };
string[] deviceIDS = { "one", "two", "three" };
string[] groupIDS = { "abc", "def", "ghi" };
array2D[0] = new string[deviceIDS.Length] deviceIDS;
array2D[1] = new string[deviceIDS.Length] groupIDS;
But it really doesn't like the last two lines, report it needs a ;
You have already created arrays here:
string[] deviceIDS = { "one", "two", "three" };
string[] groupIDS = { "abc", "def", "ghi" };
So you only need to set references to these arrays:
array2D[0] = deviceIDS;
array2D[1] = groupIDS;
Related
I need to create an array of string arrays in C#. Basically, I want to do the following, kind of a hybrid C/C#:
private string[] revA = new string[] {"one", "two", "three" };
private string[] revB = new string[] {"four", "five", "six" };
private string[] revC = new string[] {"seven", "eight", "nine" };
string[,] list = {revA, revB, revC};
string outStr = list[0][0]; // outStr == "one"
string outStr = list[2][1]; // outStr == "eight"
But I know that won't work.
I've looked into ArrayList and List, but I'm not sure how to make it work.
Any help would be appreciated.
You need to use Jagged Array string[][] instead of Multidimensional Array string[,]
This source can give you more detail about it Why we have both jagged array and multidimensional array?
string[,] list = new string[3, 3] {{"one", "two", "three" },
{"four", "five", "six" },
{"seven", "eight", "nine" } };
string outStr = list[0, 0]; // outStr == "one"
This can be possible with ArrayList and with List in C#.
I tried the following code you can check and explore more according to your requirements.
string[] reva = new string[] { "one", "two", "three" };
string[] revb = new string[] { "four", "five", "six" };
string[] revc = new string[] { "seven", "eight", "nine" };
ArrayList arrayList = new ArrayList() { reva, revb, revc };
List<string> nums = reva.Cast<string>().ToList();
nums.ForEach(Console.WriteLine);
Console.ReadLine();
Console.ReadLine();
While jagged and multidimensional arrays have been mentioned, a third option is a custom multidimensional array type. Something like
public class My2DArray<T>
{
public T[] Array { get; }
public int Width { get; }
public int Height { get; }
public My2DArray(int width, int height, params T[] initialItems)
{
Width = width;
Height = height;
Array = new T[width * height];
initialItems.CopyTo(Array, 0);
}
public T this[int x, int y]
{
get => Array[y * Width + x];
set => Array[y * Width + x] = value;
}
}
This has the advantage over multidimensional array in that the backing single dimensional array is directly accessible, and this can be good for interoperability, or if you just want to iterate over all items.
You can add constructors to initialize it however you want.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
How can I append all the string arr into one arr within a for loop?
string[] finalArray = new string[5];
for (int i = 0; i < finalArray.Length; i++)
{
string[] array1 = GenRandArray1();
string[] array2 = GenRandArray2();
string[] array3 = GenRandArray3();
string[] array4 = GenRandArray4();
// I want to combine all the string array to my finalArray.
finalArray[i] = array1 + array2 + array3 + array4
}
Since I make loop 5 times and store in finalArray[i], so the sample output will be something like this.
"12345","17896","685","984","063","991","6","9","3","6","68","20","69","52"
"43256","24356","765","345","347","983","2","1","0","5","90","34","12","76"
"76324","98754","542","625","267","865","3","2","1","8","23","43","76","86"
"00982","16864","537","847","249","136","1","0","9","3","65","80","23","17"
"12569","98754","658","345","646","999","5","9","6","3","94","63","15","47"
Do you mean concat all the arrays or a different algorithm? If you just want to concat all the arrays it just like below.
string[] finalArray = new string[5];
string[] array1 = new[] { "1", "2", "4" };
string[] array2 = new[] { "9", "3", "6" };
string[] array3 = new[] { "4", "0", "0" };
string[] array4 = new[] { "7", "8", "2" };
finalArray = finalArray.Concat(array1).Concat(array2).Concat(array3).Concat(array4).ToArray();
From your question it is not clear if you really want to concat your arrays five times, but let's assume that that is the case in your simplified code. In addition, as you remarked, you will be returning a list of arrays
Then:
List<string[]> finalArrayList = new List<string[]>();
for(int i=0; i<5; i++)
{
string[] array1 = new[] { "1", "2", "4" };
string[] array2 = new[] { "9", "3", "6" };
string[] array3 = new[] { "4", "0", "0" };
string[] array4 = new[] { "7", "8", "2" };
List<string> ConcatList = new List<string>(array1);
ConcatList.AddRange(array2);
ConcatList.AddRange(array3);
ConcatList.AddRange(array4);
finalArrayList.Add(ConcatList.ToArray());
}
var listA = new List<string> {"One", "Two", "Three"};
var listB = new List<int> {1, 2, 3};
var listC = new List<Tuple<string, int>>();
Here I've got two Lists with the same length. Is there a way to fill third listC with LINQ?
"One", 1
"Two", 2
"Three", 3
I think you are looking the Zip extension method:
var listA = new List<string> { "One", "Two", "Three" };
var listB = new List<int> { 1, 2, 3 };
var listC = listA.Zip(listB, (s, i) => new Tuple<string, int>(s, i)).ToList();
What is the best way to compare two lists based on values, order and the number of values. So all of the lists below should be different.
var list1 = new List<string> { "1", "2" };
var list2 = new List<string> { "2", "1" };
var list3 = new List<string> { "1", "2", "3" };
How about using SequenceEqual.
See http://ideone.com/yZeYRh
var a = new [] { "1", "2", "3" };
var b = new [] { "1", "2" };
var c = new [] { "2", "1" };
Console.WriteLine(a.SequenceEqual(b)); // false
Console.WriteLine(a.SequenceEqual(c)); // false
Console.WriteLine(c.SequenceEqual(b)); // false
It comes from the namespace System.Linq and can be used on any IEnumerable.
You can also pass it an IEqualityComparer to for example also do:
var d = new [] { "a", "B" };
var e = new [] { "A", "b" };
Console.WriteLine(d.SequenceEqual(e, StringComparer.OrdinalIgnoreCase)); // true
I like Zip for this, but you still need to manually compare Count.
lista.Count() ==listb.Count() && lista.Zip(listb, Equals).All(a=>a);
I am writing my testcode and I do not want wo write:
List<string> nameslist = new List<string>();
nameslist.Add("one");
nameslist.Add("two");
nameslist.Add("three");
I would love to write
List<string> nameslist = new List<string>({"one", "two", "three"});
However {"one", "two", "three"} is not an "IEnumerable string Collection". How can I initialise this in one line using the IEnumerable string Collection"?
var list = new List<string> { "One", "Two", "Three" };
Essentially the syntax is:
new List<Type> { Instance1, Instance2, Instance3 };
Which is translated by the compiler as
List<string> list = new List<string>();
list.Add("One");
list.Add("Two");
list.Add("Three");
Change the code to
List<string> nameslist = new List<string> {"one", "two", "three"};
or
List<string> nameslist = new List<string>(new[] {"one", "two", "three"});
Just lose the parenthesis:
var nameslist = new List<string> { "one", "two", "three" };
Posting this answer for folks wanting to initialize list with POCOs and also coz this is the first thing that pops up in search but all answers only for list of type string.
You can do this two ways one is directly setting the property by setter assignment or much cleaner by creating a constructor that takes in params and sets the properties.
class MObject {
public int Code { get; set; }
public string Org { get; set; }
}
List<MObject> theList = new List<MObject> { new MObject{ PASCode = 111, Org="Oracle" }, new MObject{ PASCode = 444, Org="MS"} };
OR by parameterized constructor
class MObject {
public MObject(int code, string org)
{
Code = code;
Org = org;
}
public int Code { get; set; }
public string Org { get; set; }
}
List<MObject> theList = new List<MObject> {new MObject( 111, "Oracle" ), new MObject(222,"SAP")};
This is one way.
List<int> list = new List<int>{ 1, 2, 3, 4, 5 };
This is another way.
List<int> list2 = new List<int>();
list2.Add(1);
list2.Add(2);
Same goes with strings.
Eg:
List<string> list3 = new List<string> { "Hello", "World" };
List<string> nameslist = new List<string> {"one", "two", "three"} ?
Remove the parentheses:
List<string> nameslist = new List<string> {"one", "two", "three"};
It depends which version of C# you're using, from version 3.0 onwards you can use...
List<string> nameslist = new List<string> { "one", "two", "three" };
I think this will work for int, long and string values.
List<int> list = new List<int>(new int[]{ 2, 3, 7 });
var animals = new List<string>() { "bird", "dog" };