C
C#2y ago
idris_2020

❔ How can i reference a row in a 2D array?

I have a 6x8 2D array and i want to reference a row inside that 2D array.
16 Replies
idris_2020
idris_2020OP2y ago
for example:
string[,] seats = new string[,]
{
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
};
string[,] seats = new string[,]
{
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
};
idris_2020
idris_2020OP2y ago
how can i just get
idris_2020
idris_2020OP2y ago
2nd row
Thinker
Thinker2y ago
There is no built-in way to do this, although you could write a pretty simple method for it.
idris_2020
idris_2020OP2y ago
like?
TheRanger
TheRanger2y ago
reference a row not copy it right?
idris_2020
idris_2020OP2y ago
i want to make a 1D array from a row in a 2D array
TheRanger
TheRanger2y ago
after u make that 1D array, would modifying it affect the data in the 2D array too?
idris_2020
idris_2020OP2y ago
no just want to make a copy of it basically
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
public static T[] GetRow<T>(this T[,] array, int no)
{
T[] data = new T[array.GetLength(1)];

for(int i = 0; i < array.GetLength(1); i++)
{
data[i] = array[no,i];
}

return data;
}

string[,] seats = new string[,]
{
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"1", "1", "1", "1", "1", "1"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
};

string[] row = seats.GetRow(2);

foreach(var d in row) Console.WriteLine(d);
public static T[] GetRow<T>(this T[,] array, int no)
{
T[] data = new T[array.GetLength(1)];

for(int i = 0; i < array.GetLength(1); i++)
{
data[i] = array[no,i];
}

return data;
}

string[,] seats = new string[,]
{
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"1", "1", "1", "1", "1", "1"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
};

string[] row = seats.GetRow(2);

foreach(var d in row) Console.WriteLine(d);
Console Output
1
1
1
1
1
1
1
1
1
1
1
1
Compile: 648.994ms | Execution: 112.015ms | React with ❌ to remove this embed.
TheRanger
TheRanger2y ago
well thats the 3rd row just change the index to what you need
idris_2020
idris_2020OP2y ago
im getting 2 errors when i copy and paste that
idris_2020
idris_2020OP2y ago
TheRanger
TheRanger2y ago
ah no u need to define the extension method in a static class
using System;

public class Program
{
public static void Main()
{
string[,] seats = new string[,]
{
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"1", "1", "1", "1", "1", "1"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
};

string[] row = seats.GetRow(2);

foreach(var d in row) Console.WriteLine(d);
}
}

public static class Extensions
{
public static T[] GetRow<T>(this T[,] array, int no)
{
T[] data = new T[array.GetLength(1)];

for(int i = 0; i < array.GetLength(1); i++)
{
data[i] = array[no,i];
}

return data;
}
}
using System;

public class Program
{
public static void Main()
{
string[,] seats = new string[,]
{
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"1", "1", "1", "1", "1", "1"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0"},
};

string[] row = seats.GetRow(2);

foreach(var d in row) Console.WriteLine(d);
}
}

public static class Extensions
{
public static T[] GetRow<T>(this T[,] array, int no)
{
T[] data = new T[array.GetLength(1)];

for(int i = 0; i < array.GetLength(1); i++)
{
data[i] = array[no,i];
}

return data;
}
}
a method can only be placed inside a class, it cannot be placed outside the class
idris_2020
idris_2020OP2y ago
okay that works thanks how would i make so it be able to increment through each row so it prints each row nvm i did it
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server