Skip to main content
deleted 2 characters in body
Source Link
David Gouveia
  • 25k
  • 5
  • 88
  • 127

From what I understood you're now storing the Z value as part of the Block, but you need the ability to store more than one Block in each array position.

class Block
{
    int Z;z;
}

Option 1

One possibility is to create a 2D array of ArrayList<Block> instead of Block. This way each position in the array can store more than one Block if needed. So you would create the data structure like this (I think, it's been many years since I've used Java):

ArrayList<Block>[][] data = new ArrayList<Block>[width][height];
for(int x=0; x!=width; ++x)
    for(int y=0; y!=height; ++y)
        data[x][y] = new ArrayList<Block>();

And add a new block with the add method of the ArrayList such as:

data[x][y].add(new Block(z));

This way you can have as many Block as you need in a single position. You could also use a jagged array instead of an ArrayList if you already from the start how many elements will be in that position, and it will probably have less overhead than the ArrayList.

Option 2

Another possibility is instead of using the ArrayListclass, to make the list of blocks implicit to the Block class. You could do this by adding a Next property to the class that points to the next Block that is shared by the same position. I also added an helper method that recursively adds a new Block to this implicit list:

class Block
{
    int Z;z;
    Block Next;next;

    void Add(Block block)
    {
        if(Next ==next== null) 
            Next =next= block;
        else 
            Nextnext.Add(block);
    }
}

With this you can keep using the same structure as before:

Block[][] data = new Block[width][height];

But when adding a new Block you'd do this instead:

Block block = new Block(z);
if(data[x][y] == null) 
    data[x][y] = block;
else 
    data[x][y].Add(block);

From what I understood you're now storing the Z value as part of the Block, but you need the ability to store more than one Block in each array position.

class Block
{
    int Z;
}

Option 1

One possibility is to create a 2D array of ArrayList<Block> instead of Block. This way each position in the array can store more than one Block if needed. So you would create the data structure like this (I think, it's been many years since I've used Java):

ArrayList<Block>[][] data = new ArrayList<Block>[width][height];
for(int x=0; x!=width; ++x)
    for(int y=0; y!=height; ++y)
        data[x][y] = new ArrayList<Block>();

And add a new block with the add method of the ArrayList such as:

data[x][y].add(new Block(z));

This way you can have as many Block as you need in a single position. You could also use a jagged array instead of an ArrayList if you already from the start how many elements will be in that position, and it will probably have less overhead than the ArrayList.

Option 2

Another possibility is instead of using the ArrayListclass, to make the list of blocks implicit to the Block class. You could do this by adding a Next property to the class that points to the next Block that is shared by the same position. I also added an helper method that recursively adds a new Block to this implicit list:

class Block
{
    int Z;
    Block Next;

    void Add(Block block)
    {
        if(Next == null) 
            Next = block;
        else 
            Next.Add(block);
    }
}

With this you can keep using the same structure as before:

Block[][] data = new Block[width][height];

But when adding a new Block you'd do this instead:

Block block = new Block(z);
if(data[x][y] == null) 
    data[x][y] = block;
else 
    data[x][y].Add(block);

From what I understood you're now storing the Z value as part of the Block, but you need the ability to store more than one Block in each array position.

class Block
{
    int z;
}

Option 1

One possibility is to create a 2D array of ArrayList<Block> instead of Block. This way each position in the array can store more than one Block if needed. So you would create the data structure like this (I think, it's been many years since I've used Java):

ArrayList<Block>[][] data = new ArrayList<Block>[width][height];
for(int x=0; x!=width; ++x)
    for(int y=0; y!=height; ++y)
        data[x][y] = new ArrayList<Block>();

And add a new block with the add method of the ArrayList such as:

data[x][y].add(new Block(z));

This way you can have as many Block as you need in a single position. You could also use a jagged array instead of an ArrayList if you already from the start how many elements will be in that position, and it will probably have less overhead than the ArrayList.

Option 2

Another possibility is instead of using the ArrayListclass, to make the list of blocks implicit to the Block class. You could do this by adding a Next property to the class that points to the next Block that is shared by the same position. I also added an helper method that recursively adds a new Block to this implicit list:

class Block
{
    int z;
    Block next;

    void Add(Block block)
    {
        if(next== null) 
            next= block;
        else 
            next.Add(block);
    }
}

With this you can keep using the same structure as before:

Block[][] data = new Block[width][height];

But when adding a new Block you'd do this instead:

Block block = new Block(z);
if(data[x][y] == null) 
    data[x][y] = block;
else 
    data[x][y].Add(block);
Source Link
David Gouveia
  • 25k
  • 5
  • 88
  • 127

From what I understood you're now storing the Z value as part of the Block, but you need the ability to store more than one Block in each array position.

class Block
{
    int Z;
}

Option 1

One possibility is to create a 2D array of ArrayList<Block> instead of Block. This way each position in the array can store more than one Block if needed. So you would create the data structure like this (I think, it's been many years since I've used Java):

ArrayList<Block>[][] data = new ArrayList<Block>[width][height];
for(int x=0; x!=width; ++x)
    for(int y=0; y!=height; ++y)
        data[x][y] = new ArrayList<Block>();

And add a new block with the add method of the ArrayList such as:

data[x][y].add(new Block(z));

This way you can have as many Block as you need in a single position. You could also use a jagged array instead of an ArrayList if you already from the start how many elements will be in that position, and it will probably have less overhead than the ArrayList.

Option 2

Another possibility is instead of using the ArrayListclass, to make the list of blocks implicit to the Block class. You could do this by adding a Next property to the class that points to the next Block that is shared by the same position. I also added an helper method that recursively adds a new Block to this implicit list:

class Block
{
    int Z;
    Block Next;

    void Add(Block block)
    {
        if(Next == null) 
            Next = block;
        else 
            Next.Add(block);
    }
}

With this you can keep using the same structure as before:

Block[][] data = new Block[width][height];

But when adding a new Block you'd do this instead:

Block block = new Block(z);
if(data[x][y] == null) 
    data[x][y] = block;
else 
    data[x][y].Add(block);