Search code examples
javaarraysexception2dfill

Java Arrays.fill() negative number


I am trying to fill a 2D array with '-1' as all the values. The code that I'm using is:

int c [] []=new int[4][4];
Arrays.fill(c,-1)

This throws the following error:

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer

Can anyone please tell me what's wrong in the code?


Solution

  • Its an array of array of Integer.

    You should write

        int c [] []=new int[4][4];
        for(int[] arr : c){
            Arrays.fill(arr,-1);
        }