1

I am preparing a class that can handle a hexgrid. I am trying to create a 2D array field for holding the data of that grids. I have the following code prepared. It shows no compilation error, however, one run time error is there. I am new in java programming and do not know what this mean.

Can anyone help me to fix the error or telling me a better way for this problem?

Hex grid Class (HexGrid.java)

import java.lang.reflect.Array;

public class HexGrid <T>{
    int row,col;
    T[][] grid;

    public HexGrid(T[] data){
        int n = data.length;
        int r = (int) Math.ceil(Math.sqrt(n));
        LoadData(r, r, data);
    }       
    public HexGrid(int r,T[] data){
        this(r,(int)(Math.ceil(data.length/r)),data);
    }
    public HexGrid(int r){
        this(r,r,null);
    }
    public HexGrid(int r, int c){
        this(r,c,null);
    }
    public HexGrid(int r,int c,T[] data) {
        LoadData(r, c, data);           
    }

    @SuppressWarnings("unchecked")
    public void LoadData(int r, int c, T[] data){
        Class<? extends T> cls = (Class<? extends T>) data.getClass();
        row     = r;
        col     = c;
        grid    = (T[][]) Array.newInstance(data.getClass(),r, c+(int) Math.floor(r/2.0));
        if(data!=null)
            AssignValues(data);
    }

    private void AssignValues(T[] data) {
        int dbug=0;
        for(int i=0; i<row; i++)
        {
            for(int j= (int)(Math.ceil(i/2));j<col+(int)(Math.floor(row/2)); j++)
            {
                dbug++;
                if(dbug<data.length)
                     grid[i][j] = data[i];
                else grid[i][j] = null;
            }
        }
        System.out.println(dbug+"");
    }

}

Here is a sample example how I would liked to use it (Test.java)

public class Test {

    public static void main(String[] args) {

        Integer[] data= new Integer[96];

        for(int i=0;i<96;i++)
            data[i] = i+1;

        HexGrid<Integer> objHexGrid = new HexGrid<Integer>(data);


    }

}

And the error I am receiving is the followings:

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
    at hexGrid.HexGrid.AssignValues(HexGrid.java:46)
    at hexGrid.HexGrid.LoadData(HexGrid.java:35)
    at hexGrid.HexGrid.<init>(HexGrid.java:13)
    at hexGrid.Test.main(Test.java:12)
4
  • It's well established that arrays and generics do not mix. The question I have to ask then is, do you really want an array here? Commented Jun 28, 2016 at 21:58
  • 1
    On one hand, props for using Array.newInstance instead of just casting an Object[] to whatever generic array type you want. On the other hand, you didn't actually pass in a template array to let your object know what type you're looking for. null doesn't work for that. Are you sure you wouldn't rather just declare grid as type Object[][] and assign it a non-generic array, like the standard library's generic collections do? Commented Jun 28, 2016 at 22:05
  • 3
    As a side note, these are nonsensical expressions: (int)(Math.ceil(i/2)), (int)(Math.floor(row/2)). i and row are int so the division already truncates like a floor operation. The floor is redundant and the ceil is probably a bug. Commented Jun 28, 2016 at 22:10
  • 1
    Thanks everyone. As I mentioned I am new in java programming. My requirements is to to plot a list of objects into a 2D grid with certain rules. So what is your suggestion on this. The code I have provided was the best I can do by my limited knowledge. Commented Jun 28, 2016 at 22:24

1 Answer 1

2

The problem is because you allocate an Integer[][][] array, but use it as a Integer[][].

In the following line data.getClass() returns Integer[], so you allocate a matrix of Integer[]-s instead of Integer-s:

grid = (T[][])Array.newInstance(data.getClass(),r, c+(int) Math.floor(r/2.0));

Change the above line to:

grid = (T[][])Array.newInstance(
    data.getClass().getComponentType(), r, c + r / 2);
Sign up to request clarification or add additional context in comments.

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.