Search code examples
javaarraylistunsafeunchecked

Program uses unchecked or unsafe operations for 2d ArrayList[][]


I have a task at the university to make a play board made of play fields. Every field can contain numerous items on it. I made it with an array arrayList like that:

List<String>[][] items = new ArrayList[x][y];

In Eclipse everything's OK, but when I upload it to the site of the uni it gives me an error, which I have received in other programs with other lists in it. My code in the previous programs was:

List list = new ArrayList();

and I fixed it like this:

List<String> list = new List<String>();

But now the case is different, because I am not allow to write:

List<String>[][] items = new ArrayList<String>[x][y]();

This is the error I get:

Note: student/GameImplementation.java uses unchecked or unsafe operations. 
Note: Recompile with -Xlint:unchecked for details

P.S. If you know a more elegant way to complete the task, please share it. I was thinking of something like that:

Board<Fields<Items>> board = new Board();

but have no idea how to make it work. Those objects confuse me.

Thanks in advance.


Solution

  • Best way is absolutely to encapsulate the data contained in a single cell inside a specific class:

    class Cell {
      List<String> data;
      OtherData data2;
    }
    
    Cell[][] items;
    

    What are you storing inside the List<String>? Because using a String object is not always the best solution, especially when you have a set of finite values (you could use eg an enum).