How to iterate over a multidimensional string array? - c#

I have a 2d string array (at least I think it is called a 2d array):
var target = new string[var1,var2];
Now I want to convert it to List<List<string>>:
var listlist = new List<List<string>>();
foreach (var row in target)
{
var newlist = new List<string>();
foreach (var el in row)
{
newlist.Add(el);
}
listlist.Add(newlist);
}
But row has a type is string and el has type is char.
I can't understand why el is not a string? What's wrong?

A foreach interates over a string[,] like it is a string[]. It doesn't split in rows.
If you do want to handle 'rows' and 'columns' those separately, you have to get the dimensions of the array, using the GetLength method:
var target = new string[var1, var2];
var listlist = new List<List<string>>();
for (int x = 0; x < target.GetLength(0); x++)
{
var newlist = new List<string>();
for (int y = 0; y < target.GetLength(1); y++)
{
newlist.Add(target[x, y]);
}
listlist.Add(newlist);
}

This is what you need
static void SoStrList()
{
int var1=10, var2=7;
var target=new string[var1, var2];
var listlist=new List<List<string>>();
for(int i=0; i<var1; i++)
{
var row=new List<string>();
for(int j=0; j<var2; j++)
{
row.Add(target[i, j]);
}
listlist.Add(row);
}
}

use for loop instead of foreach
var target = new string[2, 2];
target[0, 0] = "a";
target[0, 1] = "A";
target[1, 0] = "b";
target[1, 1] = "B";
var listlist = new List<List<string>>();
for (int i = 0; i < target.GetLength(0); i++)
{
var newlist = new List<string>();
for (int j = 0; j < target.GetLength(1); j++)
newlist.Add(target[i,j]);
listlist.Add(newlist);
}

Here:
foreach (var row in target)
You already have first element of your 2d array, and foreach take all chars of this elements

well ... string is array of chars.
And this confusion is what you get from using keyword var for almost everything. Its not javascript.
Secondly : You need to go for something like this
for (int i = 0; i < target.GetLength(0); i++)
{
for (int y = 0; y < target.GetLength(1); y++)
{
your manipulation with strings
}
}
but srsly... get rid of vars !!!

For LINQ lovers, the same can be achieved using the following two lines:
int R = s.GetLength(0), C = s.GetLength(1);
var MyList = Enumerable.Range(0, R).Select(i => i * C).Select(i => s.Cast<string>.Skip(i).Take(C).ToList()).ToList();

Related

How can I split data present in an array into another array.?

Basically, I am trying to split an array and want to pass its value into another array.
But, I am not able to do it.It is givin an error:
Cannot implicitly convert type String[] to string"
StreamReader EmployeeFile = new StreamReader(#"C:\Users\user\Desktop\FoodDeliverySystem\Employee_details.txt");
String ReadEmployeeData = EmployeeFile.ReadToEnd();
String[] ReadEmployeeDataByLine = ReadEmployeeData.Split(';');
for (int k = 0; k < 5; k++)
{
for (int l = 0; l < 5; l++)
{
Console.WriteLine("test");
String[,] ReadEmployeeDataByLineByCategorie = new string[k, l];
ReadEmployeeDataByLineByCategorie[k,l] = ReadEmployeeDataByLine[l].Split(',');
}
}
Since you can't be sure of how many of those values you're gonna have in each category of yours, you should use jagged arrays
That should do:
var readEmployeeDataByLine = new StreamReader(#"C:\pathToFile.txt").ReadToEnd().Split(';');
var readEmployeeDataByLineByCategorie = new string[readEmployeeDataByLine.Length][];
for (int i = 0; i < readEmployeeDataByLineByCategorie.Length; i++)
readEmployeeDataByLineByCategorie[i] = readEmployeeDataByLine[i].Split(',');
ReadEmployeeDataByLineByCategorie[k,l] is a string
while ReadEmployeeDataByLine[l].Split(',') is a string[]
string[] ReadEmployeeDataByLine = ReadEmployeeData.Split(';');
for(int i = 0;i< ReadEmployeeDataByLine.Length;i++)
{
string split = ReadEmployeeDataByLine[l].Split(',');
for(int j=0; j<split.Length;j++)
ReadEmployeeDataByLineByCategorie[i,j] = split[j]
}

C# - How to initialize multiple variables in a loop

I figured out how to do this with VBA.
Dim variable(1 To 10) As Variant
I am having a tough time figuring out how to do this with C#. Here is what I have that does not work:
for (int y = 0; y < 10; y++)
{
List<List<string>> row(y) = new List<List<string>>();
}
Any help would be appreciated.
If I understand your purpose correctly, you're trying to do this:
List<List<string>> row = new List<List<string>>(10);
for (int y = 0; y < 10; y++)
{
row.Add(new List<string>());
}
This will create a list of 10 List<string> objects and initialize each element with a new List<string> object.
Edit
After seeing your latest comment, you need this:
var rows = new List<List<string>>[10];
for (int y = 0; y < 10; y++)
{
rows[y] = new List<List<string>>();
}
Couple of ways this can be done, using a List of List<string> :
List<List<string>> list = new List<List<string>>();
for (int y = 0; y < 10; y++)
{
list.Add(new List<string>());
}
Using an array of List<string>
List<string>[] array = new List<string>[10];
for (int y = 0; y < 10; y++)
{
array[y] = new List<string>();
}

Can not figure out how to print second dimension in three dimension List

I am making lists in three dimensions. If I run my code it shows the following strings: list5 + list3 + list1. Now this is in three dimensions, so each dimension adds a word. If you look to the code there will be displayed: 4*4*4 = 64 possiblities. Now I want to add something to the second dimension: testing1 and testing2:
SecondDimonsion.Add("testing1");
SecondDimonsion.Add("testing2");
I dont want the result of testing to interact with List5. I do want them to interact with List1 tho.
So the outcome would be:
Hello1testing1
Hello2testing1
Hello3testing1
Hello4testing1
Hello1testing2
Hello2testing2
Hello3testing2
Hello4testing2
So in total it would print 64 + 8 possibilities.
Its a hard question to explain and probably even harder to understand. I would love to edit my question if anyone doenst understand the concept.
Thanks in advance!
List<string> List1 = new List<string>();
List<string> SecondDimonsion = new List<string>();
List<string> List5 = new List<string>();
List<string> List3 = new List<string>();
List<List<List<string>>> List6 = new List<List<List<string>>>();
List1.Add("Hello1");
List1.Add("Hello2");
List1.Add("Hello3");
List1.Add("Hello4");
List3.Add("123");
List3.Add("456");
List3.Add("789");
List3.Add("000");
List5.Add("white");
List5.Add("green");
List5.Add("yellow");
List5.Add("black");
SecondDimonsion.Add("testing1");
SecondDimonsion.Add("testing2");
for (int i = 0; i < List1.Count; i++)
{
List<List<string>> List2 = new List<List<string>>();
for (int j = 0; j < List3.Count -2;j++)
{
List<string> List4 = new List<string>();
for (int k = 0; k < List5.Count; k++)
{
List4.Add(List1[i] + List3[j] + List5[k]);
}
List2.Add(List4);
}
List2.Add(SecondDimonsion);
List6.Add(List2);
}
for (int k = 0; k < List1.Count; k++)
{
for (int i = 0; i < List3.Count -2; i++)
{
for (int j = 0; j < List5.Count; j++)
{
Console.WriteLine(List6[k][i][j]);
}
Console.WriteLine(List6[k][i]);
}
}
It looks like you're building 2 lists. The first one is 3d and the other is 2d. I would split it up like this:
List<string> stuff1 = new List<string>();
List<string> stuff2 = new List<string>();
List<string> stuff3 = new List<string>();
List<string> otherStuff = new List<string>();
stuff1.Add("a1");
stuff1.Add("a2");
stuff1.Add("a3");
stuff1.Add("a4");
stuff2.Add("b1");
stuff2.Add("b2");
stuff2.Add("b3");
stuff2.Add("b4");
stuff3.Add("c1");
stuff3.Add("c2");
stuff3.Add("c3");
stuff3.Add("c4");
otherStuff.Add("d1");
otherStuff.Add("d2");
List<List<List<string>>> first64 = new List<List<List<string>>>();
List<List<string>> other8 = new List<List<string>>();
foreach (var v1 in stuff1) {
// Fill temporary 2d list.
List<List<string>> list2d = new List<List<string>>();
foreach (var v2 in stuff2) {
// Fill temporary 1d list.
List<string> list1d = new List<string>();
foreach(var v3 in stuff3) {
list1d.Add(v1 + v2 + v3);
}
// Add each 1d list to the temp 2d list.
list2d.Add(list1d);
}
// Add each 2d list to the main 3d list.
first64.Add(list2d);
// Create another 1d list to hold second combinations.
List<string> otherList1d = new List<string>();
foreach(var otherV in otherStuff) {
otherList1d.Add(v1 + otherV);
}
// Add second 1d list to second 2d list.
other8.Add(otherList1d);
}
// Print first 64.
for(var x = 0; x < first64.Count; x++) {
for(var y = 0; y < first64[x].Count; y++) {
for(var z = 0; z < first64[x][y].Count; z++) {
Console.WriteLine(first64[x][y][z]);
}
}
}
// Print other 8.
for(var x = 0; x < first64.Count; x++) {
for(var y2 = 0; y2 < other8[x].Count; y2++) {
Console.WriteLine(other8[x][y2]);
}
}
If you change your last loop to:
for(var x = 0; x < List6.Count; x++) {
for(var y = 0; y < List6[x].Count; y++) {
for(var z = 0; z < List6[x][y].Count; z++) {
Console.WriteLine(List6[x][y][z]);
}
}
}
You will see that what you wrote actually does add SecondDimension's values to your list. Since List6[x].Count > List3.Count the loops never got there though. However you still haven't appended anything to them so you won't quite get the result you wanted.
Here is a fiddle you can play around in.

Remove Nulls from string[,]

I have a string array defined in c# as
string[,] options = new string[100,3];
Throughout the code it gets populated with data but not always filled.
So if I have 80 parts of it filled and 20 parts of it not filled. The 20 parts have nulls in them or 60 nulls at the end. Is there an easy way to resize the array so that after filling it the array is the same as
String[,] options = new string[80,3];
It would have to be resized based on the position of the first set of 3 nulls it found.
If this was a jagged array I would have done
options = options.Where(x => x != null).ToArray();
The method is quite long, because it has to check every row twice...
public static string[,] RemoveEmptyRows(string[,] strs)
{
int length1 = strs.GetLength(0);
int length2 = strs.GetLength(1);
// First we count the non-emtpy rows
int nonEmpty = 0;
for (int i = 0; i < length1; i++)
{
for (int j = 0; j < length2; j++)
{
if (strs[i, j] != null)
{
nonEmpty++;
break;
}
}
}
// Then we create an array of the right size
string[,] strs2 = new string[nonEmpty, length2];
for (int i1 = 0, i2 = 0; i2 < nonEmpty; i1++)
{
for (int j = 0; j < length2; j++)
{
if (strs[i1, j] != null)
{
// If the i1 row is not empty, we copy it
for (int k = 0; k < length2; k++)
{
strs2[i2, k] = strs[i1, k];
}
i2++;
break;
}
}
}
return strs2;
}
Use it like:
string[,] options = new string[100, 3];
options[1, 0] = "Foo";
options[3, 1] = "Bar";
options[90, 2] = "fiz";
options = RemoveEmptyRows(options);
As suggested by Alexei, there is another way of doing this:
public static string[,] RemoveEmptyRows2(string[,] strs)
{
int length1 = strs.GetLength(0);
int length2 = strs.GetLength(1);
// First we put somewhere a list of the indexes of the non-emtpy rows
var nonEmpty = new List<int>();
for (int i = 0; i < length1; i++)
{
for (int j = 0; j < length2; j++)
{
if (strs[i, j] != null)
{
nonEmpty.Add(i);
break;
}
}
}
// Then we create an array of the right size
string[,] strs2 = new string[nonEmpty.Count, length2];
// And we copy the rows from strs to strs2, using the nonEmpty
// list of indexes
for (int i1 = 0; i1 < nonEmpty.Count; i1++)
{
int i2 = nonEmpty[i1];
for (int j = 0; j < length2; j++)
{
strs2[i1, j] = strs[i2, j];
}
}
return strs2;
}
This one, in the tradeoff memory vs time, chooses time. It is probably faster, because it doesn't have to check every row twice, but it uses more memory, because it puts somewhere a list of the non-empty indexes.
I went for all rows until you find an row with all null values:
Needs some clean up and will obviously remove non-null rows that occur after the first all null row. The requirement wasn't too clear here
EDIT: Just seen the comment clarifying requirement to remove all null rows - I've tweaked the below to avoid downvotes but a more comprehensive answer is already accepted (and is more efficient) :)
void Main()
{
string[,] options = new string[100,3];
options[0,0] = "bleb";
options[1,1] = "bleb";
options[2,0] = "bleb";
options[2,1] = "bleb";
options[3,2] = "bleb";
options[4,1] = "bleb";
string[,] trimmed = TrimNullRows(options);
Console.WriteLine(trimmed);
}
public string[,] TrimNullRows(string[,] options)
{
IList<string[]> nonNullRows = new List<string[]>();
for (int x = 0; x < options.GetLength(0); x++)
{
bool allNull = true;
var row = new string[options.GetLength(1)];
for (int y = 0; y < options.GetLength(1); y++)
{
row[y] = options[x,y];
allNull &= options[x,y] == null;
}
if (!allNull)
{
nonNullRows.Add(row);
}
}
var optionsTrimmed = new string[nonNullRows.Count, options.GetLength(1)];
for (int i=0;i<nonNullRows.Count;i++)
{
for (int j=0;j<options.GetLength(1);j++)
{
optionsTrimmed[i, j] = nonNullRows[i][j];
}
}
return optionsTrimmed;
}
You can also get yourself some helpers to convert between jagged and multi-dimensional representations. This is pretty silly, of course, but for arrays as small as the ones you're showing (and also, very sparse arrays), it'll be fine.
void Main()
{
string[,] options = new string[100,3];
options[3, 1] = "Hi";
options[5, 0] = "Dan";
var results =
options
.JagIt()
.Where(i => i.Any(j => j != null))
.UnjagIt();
results.Dump();
}
static class Extensions
{
public static IEnumerable<IEnumerable<T>> JagIt<T>(this T[,] array)
{
for (var i = 0; i < array.GetLength(0); i++)
yield return GetRow(array, i);
}
public static IEnumerable<T> GetRow<T>(this T[,] array, int rowIndex)
{
for (var j = 0; j < array.GetLength(1); j++)
yield return array[rowIndex, j];
}
public static T[,] UnjagIt<T>(this IEnumerable<IEnumerable<T>> jagged)
{
var rows = jagged.Count();
if (rows == 0) return new T[0, 0];
var columns = jagged.Max(i => i.Count());
var array = new T[rows, columns];
var row = 0;
var column = 0;
foreach (var r in jagged)
{
column = 0;
foreach (var c in r)
{
array[row, column++] = c;
}
row++;
}
return array;
}
}
The JagIt method is pretty simple of course - we'll just iterate over the rows, and yield the individual items. This gives us an enumerable of enumerables, which we can use in LINQ quite easily. If desired, you could transform those into arrays, of course (say, Select(i => i.ToArray()).ToArray()).
The UnjagIt method is a bit more talkative, because we need to create the target array with the correct dimensions first. And there's no unyield instruction to simplify that :D
This is pretty inefficient, of course, but that isn't necessarily a problem. You could save yourself some of the iterations by keeping the inner enumerable an array, for example - that will save us having to iterate over all the inner items.
I'm mostly keeping this as the memory-cheap, CPU-intensive alternative to #xanatos' memory-intensive, CPU-cheap (relatively).
Of course, the main bonus is that it can be used to treat any multi-dimensional arrays as jagged arrays, and convert them back again. General solutions usually aren't the most efficient :D
Yet another variant with linq
static string[,] RemoveNotNullRow(string[,] o)
{
var rowLen = o.GetLength(1);
var notNullRowIndex = (from oo in o.Cast<string>().Select((x, idx) => new { idx, x })
group oo.x by oo.idx / rowLen into g
where g.Any(f => f != null)
select g.Key).ToArray();
var res = new string[notNullRowIndex.Length, rowLen];
for (int i = 0; i < notNullRowIndex.Length; i++)
{
Array.Copy(o, notNullRowIndex[i] * rowLen, res, i * rowLen, rowLen);
}
return res;
}

convert string[] to doube[,] in C#

i have text file consist of data like:
1,2
2,3
3,4
4,5
Now I want to save the data into an array. So i do split:
using (streamreader sr = new streamreader("file.txt")) {
string[] data = sr.ReadLine().Split(',');
}
however my data save in string[] while I have a GlobalDataClass array declared as double[,]. Something like this:
static class GlobalDataClass
{
public static double[,] Array = new double[4, 2];
}
I need to assign the data to the GlobalDataClass:
GlobalDataClass.Array = data;
So my question is how to convert the string[] to double[,]?
Since you have a 2-d array, you'd need to iterate over each line and extract the values, then assign it into the proper position. You can use Select and double.Parse to convert the string values to double.
using (var reader = new StreamReader("file.txt"))
{
string line;
for (var count = 0; count < 4; ++count)
{
var data = reader.ReadLine()
.Split(',')
.Select(v => double.Parse(v))
.ToArray();
GlobalDataClass.Array[count,0] = data[0];
GlobalDataclass.Array[count,1] = data[1];
}
}
Now if your array was really double[][], then you could do something more like:
GlobalDataClass.Array = File.ReadAllLines("file.txt")
.Select(l => l.Split(',')
.Select(v => double.Parse(v))
.ToArray())
.ToArray();
Note: I think it's like a really bad idea to make it a global variable. There's probably a much better way to handle this.
I think that the best way is to use Array.ConvertAll.
Example:
string[] strs = new string[] { "1.00", "2.03" };
Array.ConvertAll(strs, Double.Parse);
System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt");
int counter =0 ;
while((line = file.ReadLine()) != null)
{
var lineData= line.Split(',');
GlobalDataClass.Array[counter,0] = double.Parse(lineData[0]);
GlobalDataClass.Array[counter,1] = double.Parse(lineData[1]);
counter++;
}
Try This:
String [] words;
int lineCount=0;
String [] Lines=File.ReadAllLines(#"C:\Data.txt");
for (int i=0;i<Lines.Length;i++)
{
words = Lines[i].Split(',');
for (int j = 0; j < 2; j++)
{
GlobalDataClass.Array[i,j] = Convert.ToDouble(words[j].Trim());
}
}
I am using some part of your code to show you how you do this task.
int mCount = 0;
using (streamreader sr = new streamreader("file.txt")) {
string[] data = sr.ReadLine().Split(',');
GlobalDataClass.Array[mCount , 0] = Double.Parse(data[0]);
GlobalDataClass.Array[mCount , 1] = Double.Parse(data[1]);
mCount += 1;
}
double[] doubleArray = strArray.Select(s => Double.Parse(s)).ToArray();
int k = 0;
for (int i = GlobalDataClass.Array.GetLowerBound(0); i <= GlobalDataClass.Array.GetUpperBound(0); i++)
{
for (int j = GlobalDataClass.Array.GetLowerBound(1); j <= GlobalDataClass.Array.GetUpperBound(1); j++)
{
double d = doubleArray[k];
GlobalDataClass.Array.SetValue(d, i, j);
k++;
}
}
If the number of lines can vary:
var lines = File.ReadAllLines("file.txt");
var data = new double[lines.Length, 2];
for (var i = 0; i < lines.Length; i++)
{
var temp = lines[i].Split(',');
data[i,0] = double.Parse(temp[0]);
data[i,1] = double.Parse(temp[1]);
}
GlobalDataClass.Array = data;
..or, if the number of lines is a constant value:
using (var sr = new StreamReader("file.txt"))
{
var i = 0;
var len = GlobalDataClass.GetLength(0);
while (sr.Peak() >= 0 && i < len)
{
var temp = sr.ReadLine().Split(',');
GlobalDataClass.Array[i,0] = double.Parse(temp[0]);
GlobalDataClass.Array[i,1] = double.Parse(temp[1]);
i++;
}
}

Categories

Resources