0

This code gives me this error:"Cannot implicitly convert type ArrayList[] to ArrayList[][]" at this line: m_grid[gridIndexX] = new ArrayList[Height]; But how can i do that in another way? When m_grid array is a two dimensional array it works but as a three dimensional array it doesn't work.Thanks for help.

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);
            }
        }
    }
4
  • There is no such thing as a three-dimensional ArrayList. What you have is a three-dimensional Array of ArrayLists... Commented Jul 5, 2012 at 11:26
  • 2
    maybe it's a good idea to start defining some classes that you can have just one List<T> of? Commented Jul 5, 2012 at 11:28
  • Yes i wanted to mean it actually @codesparkle Commented Jul 5, 2012 at 11:29
  • 1
    You should use List<T> instead of the outdated ArrayList. They avoid boxing and unboxing and the compiler can perform type checks on them. They are also a lot more readable which makes it easier to work with. Commented Jul 5, 2012 at 11:34

3 Answers 3

1

You need to add another indexer. You've initialized m_grid as a 3-dimentional array. So any first-level element within m_grid is a 2-dimensional array. And you're trying to set one of those elements to a 1-dimensional array:

m_grid[gridIndexX] = new ArrayList[Height];

In the above code, m_grid[gridIndexX] is of type ArrayList[][], so you have a type mis-match.

You'll need to set it to the proper type:

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

I don't know if this alone will solve your problem, because it's difficult to discern what this code is actually supposed to do. (Indeed, if you're not sure what parts of your code are what dimensionality of arrays, I'm not sure if you even know what this code is supposed to do...)

Sign up to request clarification or add additional context in comments.

2 Comments

Actually this code is not mine and i'm trying to correct it.What i want to do is convert this array of array list to three dimensional.At the beginning m_grid array was holding particles.For example a particle(x axis:1,y axis:2) this particle's index must be in m_grid[1][2] array list.But what i want is particle(1,2,z axis:1) must be in m_grid[1][2][1] array list.I guess i'm doing it wrong:) I will try to find some other way.
@ozg: You'll probably want to look into using custom classes instead of raw arrays. A Particle class sounds like it would be very useful here. Possibly a Position struct as a property on the class, etc.
0

You are missing a []. The line

m_grid[gridIndexX] = new ArrayList[Height];

should be

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

instead.

Comments

0

You need to initialize it with a size:

ArrayList[][][] m_grid;

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

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

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

This means that your code sample will look like this:

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

Allthough I would strongly suggest you move away from ArrayList, if you can. As other commenters have said, use a generic, strongly typed collection instead such as List<T>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.