About 4,200,000 results
Open links in new tab
  1. java - Any shortcut to initialize all array elements to zero? - Stack ...

    Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10) [...] For type int, the default value is zero, that is, 0. If you want to initialize an one-dimensional array to a different value, you can use java.util.Arrays.fill () (which will of course use a loop internally).

  2. How to Initialize All Array Elements to Zero in Java - Delft Stack

    Feb 12, 2024 · In this article, readers can look forward to gaining a comprehensive understanding of various methods to re-initialize an array to zero in Java. The content will cover multiple techniques, including the use of for loops, Arrays.fill(), …

  3. How to initialize all the elements of an array to any specific value

    But in Java, how can I initialize all the elements to a specific value? Whenever we write int[] array = new int[10], this simply initializes an array of size 10 having all elements set to 0, but I just want to initialize all elements to something other than 0 (say, -1).

  4. Reinitialize all array elements to zero java - Stack Overflow

    Jun 27, 2015 · FYI, you don't need to explicity set all elements to zero. myarray=new long[size][size] will fill default values of zero in the elements. See Initial Values of Variables: Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10.2):

  5. How to Initialize an Array with 0 in Java - Java2Blog

    May 17, 2022 · In this part, we will learn how to initialize an array to zero if we already have an array declared and initialized with other values already given to us. The Util library of java …

  6. How to Initialize an Array in Java? - GeeksforGeeks

    Apr 14, 2025 · Initialize an Array in Java 1. Initialize an Array with a Fixed Size and Default Values 2. Initialize an Array with Specific Values 3. Initialize an Array Using Curly Braces { } 4. Initialize an Array with non-default values 5. Initializing an Array Using Loops Using Arrays with Unknown Size Accessing Array Elements Using Index Using Array Length

  7. Initializing Arrays in Java - Baeldung

    Dec 16, 2024 · The method Arrays.setAll () sets all elements of an array using a generator function. This method can be useful when we need to add values that follow a specific pattern or logic to an array.

  8. Any shortcut to initialize all array elements to zero? - W3docs

    To initialize all elements of an array to zero in Java, you can use the Arrays.fill method from the java.util package. For example, to initialize an array of integers to zero: This will fill the entire array with the value 0. You can also use the Arrays.fill method to initialize a part of the array to a specific value. For example:

  9. How to Initialize All Elements of an Array to Zero in Java

    Learn how to efficiently initialize a Java array to zero without using loops. Discover shortcuts and best practices for array initialization.

  10. How to initailize byte array of 100 bytes in java with all 0's

    May 10, 2013 · byte[] bytes = new byte[100]; Initializes all byte elements with default values, which for byte is 0. In fact, all elements of an array when constructed, are initialized with default values for the array element's type.

Refresh