2 dimensional array of arraylist problem

Hi, Im trying to make 9x9 table with each cell contains number 1-9. Because the cell wont always contains 9 numbers, I decided to use two dimensional Array of ArrayLists. I am able to compile and run my code below, but I am curious about the compiler warning it produced :

Dummy.java:11: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList

T[j].add((Integer)k);

What does it means? And how do I fix it?

Thank you for your help.

publicclass Dummy{

privatestatic ArrayList[][] T;

/** Creates a new instance of Dummy */

public Dummy(){

//initialize Dummy matrix

T =new ArrayList [9][9];

for (int i=0;i<T.length;i++){

for (int j=0;j<T[i].length;j++){

T[i][j] =new ArrayList ();

for (int k=1;k<=9;k++){

T[i][j].add((Integer)k);

}

}

}

}

}

=)>

[1687 byte] By [kiplingkua] at [2007-10-3 2:22:10]
# 1

Please don't use "T" as a variable name. It doesn't say a lot and violates the naming standards (variables should start with a lower case).

The warning you get is caused by not using generics. Google for a tutorial.

private List<Integer>[][] fields = new List<Integer>[9][9];

Note that I declared it as List instead of ArrayList, because it's generally better to code against the interface.

And if you just want to track which number(s) from 1-9 are set in each field (sounds a lot like Sudoku, doesn't it? ;-)) then you might consider using a BitSet (with size 9) for that information.

JoachimSauera at 2007-7-14 19:21:08 > top of Java-index,Java Essentials,New To Java...
# 2
Hi,Thanks for your answer. When I changed my code forprivate ArrayList<Integer>[][] cell = new ArrayList<Integer>[9][9];It now produces 1 compile error. ( generic array creation). Any
kiplingkua at 2007-7-14 19:21:08 > top of Java-index,Java Essentials,New To Java...
# 3
If you are just using numbers (ints) and the array is always 9x9 then just use a 2D array of ints.
sabre150a at 2007-7-14 19:21:08 > top of Java-index,Java Essentials,New To Java...
# 4

the array is indeed always 9x9 but the members of the array isnt always 1-9, it can be 1,3,5,6 or 3,4,6,7 or 1,2.

Eg :

cell[0][0] = {1,2,3,4};

cell[0][1] = {6,9};

cell[0][2] = {4,8,9};

.

.

.

cell[8][8] = {2};

At first, Im going to use 3D but since it's doesnt have fixed length so I thought it would be costly.

kiplingkua at 2007-7-14 19:21:08 > top of Java-index,Java Essentials,New To Java...
# 5

> the array is indeed always 9x9 but the members of the

> array isnt always 1-9, it can be 1,3,5,6 or 3,4,6,7

> or 1,2.

I dojn't understand!

> Eg :

> cell[0][0] = {1,2,3,4};

> cell[0][1] = {6,9};

> cell[0][2] = {4,8,9};

> .

> .

> .

> cell[8][8] = {2};

>

> At first, Im going to use 3D but since it's doesnt

> have fixed length so I thought it would be costly.

I don't understand.

sabre150a at 2007-7-14 19:21:08 > top of Java-index,Java Essentials,New To Java...
# 6

> At first, Im going to use 3D but since it's doesnt

> have fixed length so I thought it would be costly.

No, it doesn't matter wether an array has fixed dimensions or not, the costs are constant (or rather "linear with the size"), but it's not easy to handle (if you remove one item, you'll have to re-create the array).

Sorry for my advice before, I forgot that arrays and generics don't mix (which IMHO is a rather poor decision by Sun, although it might have a good reason).

My advice to use a two-dimensional array of BitSets still stands.

Then you can simply call cell[0][3].set(3) or cel[0][3].clear(3).

JoachimSauera at 2007-7-14 19:21:08 > top of Java-index,Java Essentials,New To Java...