I converted a 2D array (string[,]) to the list below successfully. Now, how can I convert the List below back to a 2D array (string[,])?
List<string[]> NameList = new List<string[]>();
This is how I converted the 2D array to list:
List<string[]> NameList = new List<string[]>();
string[,] Name2DArray = new string[Rows.Count, 3];
for (int i = 0; i < Name2DArray.GetLength(0); i++)
{
string[] temp = new string[Name2DArray.GetLength(1)];
for (int n = 0; n < temp.Length; n++)
{
temp[n] = Name2DArray[i, n];
}
NameList.Add(temp);
}
Thanks all for your suggestions. I managed to fix the problem in my code. The conversion form List string[] to 2D array is now working. Below is the code:
string[,] newArray = new string[newTotalRows, 3];
for (int i = 0; i < NameList.Count; i++)
{
for (int j = 0; j < NameList[i].Length; j++)
{
newArray[i, j] = NameList[i][j];
}
}
Related
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]
}
This is a simplified version of the problem im trying to solve. Im trying to add an int[] to a List, but it updates all the arrays in the List with the one from the last iteration. Why is that? And how does I solve this problem? This isn't a problem if it's just 1 int for example.
intArray = new int[9];
for (int i = 0; i < 9; i++)
{
intArray[i] = i;
}
Test.Add(intArray);
for (int i = 0; i < 9; i++)
{
intArray[i] = i * 2;
}
Test.Add(intArray);
foreach (var item in Test)
{
for (int i = 0; i < 9; i++)
{
Console.WriteLine(item[i]);
}
}
Console.ReadKey();
}
public static int[] intArray { get; set; }
public static List<int[]> Test = new List<int[]>();
Using new keyword you create intArray only once. Then there is only one reference. After that you add intArray reference to collection List for multiple times.
Try this,
intArray = new int[9];
for (int i = 0; i < 9; i++)
{
intArray[i] = i;
}
Test.Add(intArray);
intArray = new int[9]; // create new intArray here
for (int i = 0; i < 9; i++)
{
intArray[i] = i * 2;
}
Test.Add(intArray);
after u added your intArray object into your arraylist ,simply create another object of that
intArray = new int[length];
hope it will works
this is the text file that i want to split into a 2D array
"YHOO",36.86,21,13873900,37.00
"GOOG",684.11,1114,1821650,686.72
"MSFT",50.54,3993,31910300,50.65
"AAPL",94.40,28201,39817000,94.26
and this is the code I have implemented to do this but it won't work
String input = File.ReadAllText(#"..\..\Data\stockInfo.txt");
int i = 0, j = 0;
string[,] result = new string[3, 5];
foreach (var row in input.Split('\n'))
{
j = 0;
foreach (var col in row.Trim().Split(','))
{
result[i, j] = string.Parse(col.Trim());
j++;
}
i++;
}
Jagged array String[][] (array of array) is often more flexible than 2D one String[,]:
string[][] result = File
.ReadLines(#"..\..\Data\stockInfo.txt")
.Select(line => line.Split(','))
.ToArray();
If you insist on 2D array you have to put less efficient code
string[] lines = File.ReadAllLines(#"..\..\Data\stockInfo.txt");
string[,] result = null;
for (int i = 0; i < lines.Count; ++i) {
string[] items = lines.Split(',');
if (null == result)
result = new string[lines.Count, items.Length];
for (int j = 0; j < items.Length; ++j)
result[i, j] = items[j];
}
The size of array was wrong. Also, you don't need to string.Parse, as output of Split is IEnumerable of strings
int i = 0, j = 0;
string[,] result = new string[4, 5];
foreach (var row in input.Split('\n'))
{
j = 0;
foreach (var col in row.Trim().Split(','))
{
result[i, j] = col.Trim();
j++;
}
i++;
}
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.
I need to create a list of integer arrays. I know ahead of time the length of the arrays, but I don't know how many of them need to be added to the list.
I've tried the following code:
List<int[]> MyListOfArrays = new List<int[]>();
int[] temp = new int[30];
range = xlApp.get_Range("NamedRange");
values = (object[,])range.Value2;
for (int i = 0; i < values.GetLength(0); i++)
{
for (int j = 0; j < values.GetLength(1); j++)
{
temp[j] = Convert.ToInt32(values[i + 1, j + 1]);
}
MyListOfArrays.Add(temp);
}
The temp array is filled just fine. However, MyListOfArrays just ends up with the last iteration of temp repeated for all of the entries. Where am I going wrong?
When you add the temp array to the List, it is just a pointer to the array created on the heap. You need to create a new temp array for every array you add to the list.
List<int[]> MyListOfArrays = new List<int[]>();
range = xlApp.get_Range("NamedRange");
values = (object[,])range.Value2;
for (int i = 0; i < values.GetLength(0); i++)
{
int[] temp = new int[30]; // moved inside the loop
for (int j = 0; j < values.GetLength(1); j++)
{
temp[j] = Convert.ToInt32(values[i + 1, j + 1]);
}
MyListOfArrays.Add(temp);
}
Here's the easiest fix.
Your code:
List<int[]> MyListOfArrays = new List<int[]>();
int[] temp = new int[30];// <-- Move this inside the 'i' for-loop.
range = xlApp.get_Range("NamedRange");
values = (object[,])range.Value2;
for (int i = 0; i < values.GetLength(0); i++)
{
for (int j = 0; j < values.GetLength(1); j++)
{
temp[j] = Convert.ToInt32(values[i + 1, j + 1]);
}
MyListOfArrays.Add(temp);
}
Do this instead:
List<int[]> MyListOfArrays = new List<int[]>();
range = xlApp.get_Range("NamedRange");
values = (object[,])range.Value2;
for (int i = 0; i < values.GetLength(0); i++)
{
int[] temp = new int[30]; //<-- This will create a new array of ints, with each iteration of 1.
for (int j = 0; j < values.GetLength(1); j++)
{
temp[j] = Convert.ToInt32(values[i + 1, j + 1]);
}
MyListOfArrays.Add(temp);
}
You just need to move the line:
int[] temp = new int[30];
immediately after:
for (int i = 0; i < values.GetLength(0); i++) {
so it initializes to a new array at each iteration of the loop.
MyListOfArrays.Add is adding the same reference to you list for each iteration of i, and it overwrites the values in temp[] each time. So you end up with the values of the last iteration repeated.