Arrays.fill with multidimensional array in Java

后端 未结 13 1748
心在旅途
心在旅途 2020-11-27 18:49

How can I fill a multidimensional array in Java without using a loop? I\'ve tried:

double[][] arr = new double[20][4];
Arrays.fill(arr, 0);

相关标签:
13条回答
  • 2020-11-27 19:23

    This is because a double[][] is an array of double[] which you can't assign 0.0 to (it would be like doing double[] vector = 0.0). In fact, Java has no true multidimensional arrays.

    As it happens, 0.0 is the default value for doubles in Java, thus the matrix will actually already be filled with zeros when you get it from new. However, if you wanted to fill it with, say, 1.0 you could do the following:

    I don't believe the API provides a method to solve this without using a loop. It's simple enough however to do it with a for-each loop.

    double[][] matrix = new double[20][4];
    
    // Fill each row with 1.0
    for (double[] row: matrix)
        Arrays.fill(row, 1.0);
    
    0 讨论(0)
  • 2020-11-27 19:23

    Using Java 8, you can declare and initialize a two-dimensional array without using a (explicit) loop as follows:

    int x = 20; // first dimension
    int y = 4; // second dimension
    
    double[][] a = IntStream.range(0, x)
                            .mapToObj(i -> new double[y])
                            .toArray(i -> new double[x][]);
    

    This will initialize the arrays with default values (0.0 in the case of double).

    In case you want to explicitly define the fill value to be used, You can add in a DoubleStream:

    int x = 20; // first dimension
    int y = 4; // second dimension
    double v = 5.0; // fill value
    
    double[][] a = IntStream
            .range(0, x)
            .mapToObj(i -> DoubleStream.generate(() -> v).limit(y).toArray())
            .toArray(i -> new double[x][]);
    
    0 讨论(0)
  • 2020-11-27 19:25
    double[][] arr = new double[20][4];
    Arrays.fill(arr[0], 0);
    Arrays.fill(arr[1], 0);
    Arrays.fill(arr[2], 0);
    Arrays.fill(arr[3], 0);
    Arrays.fill(arr[4], 0);
    Arrays.fill(arr[5], 0);
    Arrays.fill(arr[6], 0);
    Arrays.fill(arr[7], 0);
    Arrays.fill(arr[8], 0);
    Arrays.fill(arr[9], 0);
    Arrays.fill(arr[10], 0);
    Arrays.fill(arr[11], 0);
    Arrays.fill(arr[12], 0);
    Arrays.fill(arr[13], 0);
    Arrays.fill(arr[14], 0);
    Arrays.fill(arr[15], 0);
    Arrays.fill(arr[16], 0);
    Arrays.fill(arr[17], 0);
    Arrays.fill(arr[18], 0);
    Arrays.fill(arr[19], 0);
    
    0 讨论(0)
  • 2020-11-27 19:27

    Don't we all sometimes wish there was a
    <T>void java.util.Arrays.deepFill(T[]…multiDimensional). Problems start with
    Object threeByThree[][] = new Object[3][3];
    threeByThree[1] = null; and
    threeByThree[2][1] = new int[]{42}; being perfectly legal.
    (If only Object twoDim[]final[] was legal and well defined…)
    (Using one of the public methods from below keeps loops from the calling source code.
    If you insist on using no loops at all, substitute the loops and the call to Arrays.fill()(!) using recursion.)

    /** Fills matrix {@code m} with {@code value}.
     * @return {@code m}'s dimensionality.
     * @throws java.lang.ArrayStoreException if the component type
     *  of a subarray of non-zero length at the bottom level
     *  doesn't agree with {@code value}'s type. */
    public static <T>int deepFill(Object[] m, T value) {
        Class<?> components; 
        if (null == m ||
            null == (components = m.getClass().getComponentType()))
            return 0;
        int dim = 0;
        do
            dim++;
        while (null != (components = components.getComponentType()));
        filler((Object[][])m, value, dim);
        return dim;
    }
    /** Fills matrix {@code m} with {@code value}.
     * @throws java.lang.ArrayStoreException if the component type
     *  of a subarray of non-zero length at level {@code dimensions}
     *  doesn't agree with {@code value}'s type. */
    public static <T>void fill(Object[] m, T value, int dimensions) {
        if (null != m)
            filler(m, value, dimensions);
    }
    
    static <T>void filler(Object[] m, T value, int toGo) {
        if (--toGo <= 0)
            java.util.Arrays.fill(m, value);
        else
            for (Object[] subArray : (Object[][])m)
                if (null != subArray)
                    filler(subArray, value, toGo);
    }
    
    0 讨论(0)
  • 2020-11-27 19:28
    public static Object[] fillArray(Object[] arr,Object item){
        Arrays.fill(arr, item);
        return arr;
    }
    Character[][] maze = new Character[10][10];
        fillArray(maze, fillArray(maze[0], '?'));
    
        for(int i = 0;i<10;i++){
            System.out.println();
            for(int j = 0;j<10;j++){
                System.out.print(maze[i][j]);
            }
        }
    

    i hope this do well

    0 讨论(0)
  • 2020-11-27 19:29

    As Per Java 8, we can use this way.

    double[][] arr = new double[20][4]; Arrays.stream(arr).forEach(a -> Arrays.fill(a, 0));

    We can initialize a value in multidimensional array in a nicer and smart way.

    0 讨论(0)
提交回复
热议问题