0

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.

Donald_W
  • 1,773
  • 21
  • 35

1 Answers1

1

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).

Jack
  • 131,802
  • 30
  • 241
  • 343
  • I am storing the names of the items on the current field. The items are finite, but are being initialized every game and can be different each game. This means there are addItem methods, so I think that enum cannot me used in my case. – Daniel Dimitrov Mar 20 '13 at 00:23
  • @DanielDimitrov I don't see an enum mentioned anywhere in Jack's answer. – Jim Garrison Mar 20 '13 at 03:46