【问题标题】:How to get elements in Jagged array in C# (UNITY 3d)如何在 C# 中获取锯齿状数组中的元素(UNITY 3d)
【发布时间】:2016-10-10 08:52:39
【问题描述】:

我创建了一个 3d int 数组

public int[,,]  npcState =  new int[,,] {
    {
        {0,0}
    },{
        {1,9,1},{1,0,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
        {1,1,1},{10,10}
    },{
        {8,0},{0,0},{0,0},{0,0}
    },{
        {10,7},{1,1,1},{1,1,1},{1,1,1},{1,1,1}
    },{
        {2,2,2}
    },{
        {1,1,1} ,{1,1,1}
    },{
        {8,11},{0,0},{0,0},{0,0},{0,0}
    },{
        {0,1,1},{1,1,1}
    }
};

我的问题是

1.) 如何在运行时赋值

2.)如何使用循环检查数组的每一行和每一列

for(int i =0 ; i < firstDimensionalLength ; i ++){
  for(int j =0 ; j < secondDimensionalLength; j ++){
     for(int k =0 ; k < thirdDimensionalLength; k ++){
// print (npcState[i,j,k]); 
   }
  }
}

如果它的所有维度的长度都是恒定的,那么很容易找到元素。但是如果它是动态的,如何在特定位置找到每个元素

【问题讨论】:

  • 你不能像那样定义一个 3D 数组。所有内部数组的长度必须相同。你只能用锯齿状数组来做到这一点。
  • 您的代码无法编译。先解决这个问题。

标签: c# arrays unity3d


【解决方案1】:

编辑:根据评论者的建议,我正在添加多维数组声明的编译版本:

public int[,,] npcState =  new int[,,] {
    {
       {2,2,2},{1,1,1},{1,1,1}
    },{
        {1,9,1},{1,0,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
       {2,2,2},{1,1,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    }
};

如果你真的想使用for循环,那么你可以使用GetLength()方法来获取维度的长度:

var firstDimensionalLength = npcState.GetLength(0);
var secondDimensionalLength = npcState.GetLength(1);
var thirdDimensionalLength = npcState.GetLength(2);

Source

【讨论】:

  • 我建议还添加一个初始化这样一个数组的编译版本,因为他的问题中的那个是无效的
【解决方案2】:

如果您只想扫描整个阵列,请尝试使用foreach

foreach (int item in npcState) {
  // print (item); 

  if (SomeCondition(item)) {
    ...
  }  
}

请注意,循环不依赖于数组的维度(对于 1d、2d、3d 等数组来说都是一样的)

编辑:如果您想要项目的位置(即i, j, k 索引),您必须输入

// In many cases you can put 0 instead of `npcState.GetLowerBound()` since 
// arrays are zero based by default
for (int i = npcState.GetLowerBound(0); i <= npcState.GetUpperBound(0); i++)
  for (int j = npcState.GetLowerBound(1); j <= npcState.GetUpperBound(1); ++j)
    for (int k = npcState.GetLowerBound(2); k <= npcState.GetUpperBound(2); ++k) {
      int item = npcState[i, j, k];
      ...
    }

Edit 2:由于问题已被编辑,并且 ND 数组已变成 jagged 之一,因此也应该更改解决方案:

  int[][][] npcState = new int[][][] {
    new int[][] {
      new int[] { 0, 0 } },
    new int[][] {
      new int[] { 1, 9, 1},
      new int[] { 1, 0, 1},
      new int[] { 1, 1, 1}, },
    new int[][] {
      new int[] { 2, 2, 2},
      new int[] { 1, 1, 1},
      new int[] { 1, 1, 1}, }, 
    // ...
  };

 // Array of array of array can be just flatten twice
 foreach (var item in npcState.SelectMany(line => line.SelectMany(row => row))) {
   ...
 }

位置保留循环将是

 for (int i = 0; i < npcState.Length; ++i) 
   for (int j = 0; j < npcState[i].Length; ++j) 
     for (int k = 0; k < npcState[i][j].Length; ++k) {
       int item = npcState[i][j][k];
       ...   
     }

【讨论】:

  • 这段代码将给出每个元素,我需要每个元素在特定位置
  • 如何在数组中的特定位置找到特定元素。
  • @Liju Thomas:如果您不仅想要item 本身,还想要它的位置(即ijk 索引)你必须扫描尺寸:例如for (int i = npcState.GetLowerBound(0); i &lt;= npcState.GetUpperBound(0); i++) 第一个(见我的编辑)
  • @Liju Thomas:请问什么不起作用?你有例外吗?代码无法编译;结果与预期不符?
  • 结果为 (0,0,0) = 0 (0,0,1) = 0 (1,0,0) = 1 (1,0,1) = 9 。没有获得第三 (1,0,2) 。每行只得到两个元素
猜你喜欢
  • 2011-06-10
  • 1970-01-01
  • 2015-11-30
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 2017-04-19
  • 2023-03-25
  • 1970-01-01
相关资源
最近更新 更多