【问题标题】:Cannot implicitly convert type ArrayList[] to ArrayList[][]无法将类型 ArrayList[] 隐式转换为 ArrayList[][]
【发布时间】:2012-07-05 19:06:47
【问题描述】:

这段代码给了我这个错误:“无法将类型 ArrayList[] 隐式转换为 ArrayList[][]”在这一行:m_grid[gridIndexX] = new ArrayList[Height]; 但我该如何以另一种方式做到这一点?当 m_grid 数组是二维数组时它可以工作,但作为一个三维数组它不起作用。谢谢您的帮助。

private ArrayList[][][] m_grid;
private void initialize() {
    Width           = 5;
    Height          = 5;
    Depth           = 5;
    m_grid = new ArrayList[Width][][];
}
public void Refresh(ref ArrayList particles) {

        m_grid = null;
        m_grid = new ArrayList[Width][][];

        if (particles != null) {
            for (int i = 0; i < particles.Count; i++) {
                FluidParticle p = (FluidParticle) particles[i];
                int gridIndexX = GetGridIndexX(ref p);
                int gridIndexY = GetGridIndexY(ref p);
                int gridIndexZ = GetGridIndexZ(ref p);

                // Add particle to list
                if (m_grid[gridIndexX] == null) {
                    m_grid[gridIndexX] = new ArrayList[Height];
                }
                if (m_grid[gridIndexX][gridIndexY][gridIndexZ] == null) {
                    m_grid[gridIndexX][gridIndexY][gridIndexZ] = new ArrayList();
                }
                m_grid[gridIndexX][gridIndexY][gridIndexZ].Add(i);
            }
        }
    }

【问题讨论】:

  • 没有三维ArrayList这样的东西。你所拥有的是一个三维 ArrayLists 数组...
  • 也许开始定义一些你只能拥有一个 List 的类是个好主意?
  • 是的,我实际上是想说@codesparkle
  • 您应该使用List&lt;T&gt; 而不是过时的ArrayList。它们避免装箱和拆箱,编译器可以对它们执行类型检查。它们也更具可读性,使其更易于使用。
  • 好的,我会尝试使用它,感谢您的建议

标签: c# multidimensional-array arraylist


【解决方案1】:

您需要添加另一个索引器。您已将 m_grid 初始化为 3 维数组。所以m_grid 中的任何一级元素都是二维数组。而您正试图将其中一个元素设置为一维数组:

m_grid[gridIndexX] = new ArrayList[Height];

在上面的代码中,m_grid[gridIndexX] 的类型是 ArrayList[][],所以你的类型不匹配。

您需要将其设置为正确的类型:

m_grid[gridIndexX] = new ArrayList[Height][];

我不知道仅此一项是否会解决您的问题,因为很难辨别这段代码实际上应该做什么。 (确实,如果您不确定代码的哪些部分是数组的什么维度,我不确定 是否知道这段代码应该做什么......)

【讨论】:

  • 实际上这段代码不是我的,我正在尝试纠正它。我想做的是将这个数组列表的数组转换为三维。开始时 m_grid 数组是持有粒子的。例如一个粒子(x 轴:1,y 轴:2)这个粒子的索引必须在 m_grid[1][2] 数组列表中。但我想要的是粒子(1,2,z 轴:1)必须在 m_grid[ 1][2][1] 数组列表。我想我做错了:) 我会尝试找到其他方法。
  • @ozg:您可能希望研究使用自定义类而不是原始数组。 Particle 类听起来在这里非常有用。可能是 Position 结构作为类的属性等。
【解决方案2】:

您缺少[]。线

m_grid[gridIndexX] = new ArrayList[Height];

应该是

m_grid[gridIndexX] = new ArrayList[Height][];

改为。

【讨论】:

    【解决方案3】:

    你需要用一个大小来初始化它:

    ArrayList[][][] m_grid;
    
    m_grid = new ArrayList[100][][];
    
    m_grid[0] = new ArrayList[100][];
    
    m_grid[0][0] = new ArrayList[100];
    

    这意味着您的代码示例将如下所示:

    public void Refresh(ref ArrayList particles)
    {
    
        m_grid = null;
        m_grid = new ArrayList[Width][][];
    
        if (particles != null)
        {
            for (int i = 0; i < particles.Count; i++)
            {
                FluidParticle p = (FluidParticle)particles[i];
                int gridIndexX = GetGridIndexX(ref p);
                int gridIndexY = GetGridIndexY(ref p);
                int gridIndexZ = GetGridIndexZ(ref p);
    
                // Add particle to list
                if (m_grid[gridIndexX] == null)
                {
                    m_grid[gridIndexX] = new ArrayList[Height][];
                }
                if (m_grid[gridIndexX][gridIndexY][gridIndexZ] == null)
                {
                    m_grid[gridIndexX][gridIndexY][gridIndexZ] = new ArrayList();
                }
                m_grid[gridIndexX][gridIndexY][gridIndexZ].Add(i);
            }
        }
    }
    

    尽管我强烈建议你离开ArrayList,如果可以的话。正如其他评论者所说,请改用通用的强类型集合,例如List&lt;T&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-22
      • 2012-04-25
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多