C# - How to initialize multiple variables in a loop - c#

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>();
}

Related

How to print C# 3D jagged array

From #Henk Holterman's response regarding C# 3 dimensional arrays (answered Mar 29 '09 at 12:05), how do you print foos to the console:
Foo[][][] foos = new Foo[2][][];
for (int a = 0; a < foos.Length; a++)
{
foos[a] = new Foo[3][];
for (int b = 0; b < foos[a].Length; b++)
{
foos[a][b] = new Foo [4];
for (int c = 0; c < foos[a][b].Length; c++)
foos[a][b][c] = new Foo();
}
}
Thanks.
This is fairly simple to do. Use three for statements to loop through each indexer to get to each instance of Foos.
for (int x = 0; x < foos.Length; x++) {
for (int y = 0; y < foos[x].Length; y++) {
for (int z = 0; z < foos[x][y].Length; z++) {
Console.WriteLine(foos[x][y][z].Member);
}
}
}

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.

How to iterate over a multidimensional string array?

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

Get objects at index out of List<Object>

I'm trying to make a 2D RPG game, and would like to use a List< List< Object > > to make a grid in which I store world objects of different types. Only problem is, that I don't know how to get stuff out of this multidimensional List.
The code below creates a multidimensional List and fills it with 'Dirt' Objects (though objects[i] doesnt work, which is my problem).
public List<List<Object>> objects;
this.mapWidth = 36;
this.mapHeight = 21;
this.objects = new List<List<Object>>();
for (int y = 0; y < mapHeight; y++)
{
objects.Add(new List<Object>());
}
for (var i = 0; i < mapWidth; i++)
{
for (var j = 0; j < mapHeight; j++)
{
objects[i].Add(new Dirt());
}
}
The players has a position, e.g. 18,11, which is in the middle of this map/multidimensional List. I would like to check which 'tiles' are around him so I only have to update those things on the map. List[y][x] doesnt work.
I think your initial construction code is incorrect. Looks like you're building your grid to have mapHeight rows and mapheight columns. Rewrite the construction code to be this:
for (var y = 0; y < mapHeight; y++)
{
objects.Add(new List<Object>());
for (var x = 0; x < mapWidth; x++)
{
objects[y].Add(new Dirt());
}
}
EDIT: And I think this will produce a table where your lookup can be achieved by: Object gridEntry = objects[y][x];
EDITx2: If you like, you can rewrite the creation code to this:
for (var y = 0; y < mapHeight; y++)
{
List<Object> currentRow = new List<Object>();
objects.Add(currentRow);
for (var x = 0; x < mapWidth; x++)
{
currentRow.Add(new Dirt());
}
}

Initializing a jagged array in c# gives index out of range exception

field = new int[input.Width][][];
for (int x = 0; x < input.Width; x++)
{
field[x] = new int[input.Height][];
for (int y = 0; y < 3; y++)
{
field[x][y] = new int[3];
}
}
For some reason the above code is giving me out of range exception but the following works fine:
field = new int[input.Width][][];
for (int x = 0; x < input.Width; x++)
{
field[x] = new int[input.Height][];
}
for (int y = 0; y < input.Height; y++)
{
for (int x = 0; x < input.Width; x++)
{
field[x][y] = new int[3];
field[x][y][0] = random.Next(0, output.Width);
field[x][y][1] = random.Next(0, output.Height);
field[x][y][2] = MaskFunctions.DSCALE;
}
}
Can anyone point out what am i doing wrong?
Also: Is there a difference between out of range and out of bound exception?
Your inner loop goes from 0 to 3, instead of 0 to input.Height. This will produce an out of range exception when input.Height < 3.
You probably meant to do this:
field = new int[input.Width][][];
for (int x = 0; x < input.Width; x++)
{
field[x] = new int[input.Height][];
for (int y = 0; y < input.Height; y++)
{
field[x][y] = new int[3];
}
}

Categories

Resources