About 92,400 results
Open links in new tab
  1. How do I fill arrays in Java? - Stack Overflow

    Feb 23, 2009 · Array elements in Java are initialized to default values when created. For numbers this means they are initialized to 0, for references they are null and for booleans they are false. To fill the array with something else you can use Arrays.fill() …

  2. Java Arrays.fill() - Stack Overflow

    Mar 9, 2011 · First, note that 0 is the default value for int arrays, so you don't have to fill something with 0. If you want your array to stay truly multidimensional, you'll need a loop. public static void fill(int[][] array, int element) { for(int[] subarray : array) { Arrays.fill(subarray, element); } }

  3. Arrays.fill with multidimensional array in Java - Stack Overflow

    Nov 24, 2019 · @Caroline:If you are trying to initialize the 2d array with 0,u need not do so as it is already initialized with 0 when you allocate the array,and you cannot initialize any array without using a loop.You can just hide the loop in a function just as Arrays.fill does.

  4. How to autofill an array in java..? - Stack Overflow

    Dec 8, 2012 · For one dimensional array you can use. int[] array = new int[9000]; Arrays.fill(array, -1) but for multidimensional array you will have to use a loop. Your requirement of not using a loop sounds rather arbitrary, because in fact even in the case of a one dimensional array you are using a loop. It is just hidden away in the fill method. At a ...

  5. Java 8 fill array with supplier - Stack Overflow

    Aug 1, 2014 · In java.util.Arrays there is <T> void Arrays.setAll(T[] array, IntFunction<T> generator) This doesn't take a supplier; instead it takes an IntFunction whose input argument is the array index being filled. If your objects aren't dependent upon the destination array index, you can disregard the parameter and call a supplier like this:

  6. java - Any shortcut to initialize all array elements to zero? - Stack ...

    According to the Java Language specification, section 15.10.2, if an array is created with an array creation exception that does not provide initial values, then all the elements of the array are initialized to the default value for the array's component type - i.e. 0 in the case of char[]. This is a guarantee; I'd be quite surprised of Oracle ...

  7. java - shortest way of filling an array with 1,2...n - Stack Overflow

    Apr 15, 2013 · Another alternative if you use Java 8: int[] array = new int[100]; Arrays.setAll(array, i -> i + 1); The lambda expression accepts the index of the cell, and returns a value to put in that cell. In this case, cells 0 - 99 are assigned the values 1-100.

  8. java - Fill arrays with ranges of numbers - Stack Overflow

    As for the first question, whether it is possible to fill an array with the values of a range: it is actually possible to achieve that with the combination of Range, DiscreteDomain, ContiguousSet and Ints from Guava: int[] array = Ints.toArray( ContiguousSet.create(Range.closed(1, 500), DiscreteDomain.integers()));

  9. java - Fastest way to set all values of an array? - Stack Overflow

    Feb 3, 2012 · For a small array, a simple loop is faster than the System.arraycopy approach, because of the overhead associated with setting up System.arraycopy. Therefore, it's better to fill the first few bytes of the array using a simple loop, and only move to System.arraycopy when the filled array has a certain size.

  10. java - Fill array with elements from another array - Stack Overflow

    Oct 2, 2018 · Hey I would like to fill an array from elements from another array. For example (below) And array b should be filled with elements in this order from array b.

Refresh