0

I'm doing this in my java file:

ArrayList<String> Matrix[][] = new ArrayList[n][n];

even if it works, i get this warning:

Type safety: The expression of type ArrayList[][] needs unchecked conversion to conform to ArrayList[][]

What's the problem? Thank you.

1
  • Why are you naming your variables with an uppercase first character? Commented Jan 29, 2014 at 16:42

1 Answer 1

0

You haven't initialised it with a generic type.

Using Java 7 you can use the diamond. With Java 6 and below you have to initialise it with String as the generic type.

If you want to create an arraylist holding a two-dimensional array you do this:

ArrayList<String[][]> matrix = new ArrayList<>();

EDIT: I think you're trying to create an array of arraylists? It's better to declare it like so:

ArrayList<String>[][] matrix = new ArrayList<String>[][]();
Sign up to request clarification or add additional context in comments.

12 Comments

That wouldn't even compile.
Only the third option, ArrayList<String[][]> matrix = new ArrayList<>() will compile.
@Someone I would suggest you to go through the answer in the duplicate question to understand what is going on.
@RohitJain It's compiling for me. Also it makes sense to me. The other methods attempt to use the c-style array around the ArrayList, whereas that method attempts to create a normal ArrayList that contains a c-style array of Strings.
This compiles and works as expected: ArrayList<String[][]> matrix3 = new ArrayList<>(); String[] stringArr1= {"one", "two", "three"}; String[] stringArr2= {"one", "two", "three"}; String[] stringArr3= {"one", "two", "three"}; String[][] string2dArr = {stringArr1, stringArr2, stringArr3}; matrix3.add(string2dArr);
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.