【发布时间】:2021-12-23 20:20:43
【问题描述】:
我目前正在进行一个项目,我必须将仓库转换为数组表示,但是,我遇到了问题。我想将工作人员可以行走的路径定义为值为 1 的数组单元。工作人员不能去的地方的值为 0,带有物品的架子将为对象。这当然不是一个可行的选择,因为我们不能将对象存储在 int 数组中,但是,除了使用 int 数组之外,我目前想不出另一种表示路径的方法。那么如何去做呢? 这是我正在寻找的内容的快速可视化表示:Array example
namespace MyApplication
{
class MagazijnObject
{
public string Locatie;//waar ligt het product?
public int Ranking; //hoe populair is dit product?
public string Item; //welk product ligt hier?
public MagazijnObject(string loc, int rank, string it)
{
Locatie = loc;
Ranking = rank;
Item = it;
}
static void Main(string[] args)
{
MagazijnObject voorbeeld = new MagazijnObject("D02020A", 1, "Meel");
Console.WriteLine(voorbeeld.Locatie);
Console.WriteLine(voorbeeld.Ranking);
Console.WriteLine(voorbeeld.Item);
int[,] GangVoorbeeld = new int[5,10]; //this is a single hallway, with shelves on either side
string gang = "D02";
//traverse array
for (int i = 0; i < GangVoorbeeld.GetLength(0); i++)
{for (int j = 0; j < GangVoorbeeld.GetLength(1); j++)
{ if ( i == 1 || i == 3) //shelves with items
{GangVoorbeeld[i,j] = new MagazijnObject(gang + i, 1, "test"); //try to create object, however can't do so because I have an int array. However, I don't know how to define the walking path in an object representation.
}
}
}
}
}}
任何提示将不胜感激! 亲切的问候, Douwe Brink
【问题讨论】:
-
也许你想要
Dictionary<int, object>()?还是List<(int, object)>()?或者创建一些具有 int 和 object 属性的 Model 类,并在List<Model>()中使用它。 -
@Auditive 我想保留数组表示,所以字典不起作用。还有其他想法吗?
-
如果你设置在一个数组上,你可以让它成为你想要使用的一些共同父级的数组。最简单的当然是
object[][]或object[,](呃二维数组)
标签: c# arrays object path-finding