Reputation: 391
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);
}
}
}
Upvotes: 0
Views: 1100
Reputation: 36287
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>
.
Upvotes: 0
Reputation: 218837
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...)
Upvotes: 1
Reputation: 3972
You are missing a []
. The line
m_grid[gridIndexX] = new ArrayList[Height];
should be
m_grid[gridIndexX] = new ArrayList[Height][];
instead.
Upvotes: 0