4

Can I initialize ArrayList of 2D array, is this a thing?

So when I try to initialize this, below is my code

ArrayList<int>[][] suffle = new ArrayList<int>[row][col]; 

I get an error like this:

Error: Syntax error, insert "Dimensions" to complete ReferenceType

How can I fix this?

3
  • While my answer below does satisfy the syntax error, I can't help but wonder why you think you need a two-dimensional array of ArrayLists. Commented Jan 18, 2015 at 3:43
  • It is a project that I have to do for AP CS Picture Lab where my teacher asks us to Divide a picture into N rows and M columns, then shuffle (randomly) all the rectangles and display the new picture. The user should enter N and M, so I am thinking about using a 2d array to store the (startrow, endrow, startcol, endcol) (so an arraylist of intergers) to solve this problem. Commented Jan 18, 2015 at 3:47
  • 1
    You'd probably want to stick to arrays, then. Commented Jan 18, 2015 at 3:47

4 Answers 4

5

It is a thing, but you have to use an object, not a primitive. This applies to all generic types.

ArrayList<Integer>[][] suffle = new ArrayList[row][col];

You're going to get some compiler warnings about the above declaration, but it is perfectly possible to do.

Depending on what it is you're doing, it might be better to use a list of lists, which will ensure type safety as oppose to the unchecked warning you'd get from the above...

List<List<Integer>> suffle = new ArrayList<>();

...or a standard two-dimensional array:

int[][] suffle = new int[row][col];
Sign up to request clarification or add additional context in comments.

2 Comments

Why use a raw ArrayList instead of writing new ArrayList<Integer>[row][col]?
Because that declaration isn't legal.
2

You can also stick entirely with primitives, i.e.

 int[][] i = new int[row][col]; 

Or mix and match a list of int[]

ArrayList<int[]> al = new ArrayList<>();

And almost an array of lists:

/* writing new ArrayList<Integer>[1], which is what 
you'd want to do, is not allowed for array creation.*/
ArrayList<Integer>[] a = new ArrayList[1]; 

Comments

0

Yes, you can make a 2-dimensional array of Object types (in this case ArrayList).

But you need to write it as:

ArrayList<Integer>[][] suffle = new ArrayList[row][col];

Also make sure you initialize row and col as integer values, before you initialize the array list.

The individual elements of suffle will be declared as ArrayList types, but not initialized. You'll need to individually initialize them.

Comments

0

Wherever you need to use generic types within diamond brackets, you should not use primitive types such as int, double etc. Rather, respective wrapper types such as Integer and Double etc should be used.

After I saw this question being asked in many forums, I have explained this with examples on my blog as well - http://www.javabrahman.com/corejava/how-to-resolve-syntax-error-insert-dimensions-to-complete-referencetype/

(Note/Disclosure - the above link is from a blog owned by me)

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.