If it's a primitive type, you can use Arrays.fill():

Arrays.fill(array, -1);

[Incidentally, memset in C or C++ is only of any real use for arrays of char.]

Answer from Oliver Charlesworth on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java array › initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - In the code above, we initialize a numbers array to hold seven elements of int type. After initialization, we can assign values to individual elements using their indices: ... Here, we add 10 and 20 to indices 0 and 1, respectively.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
... Elements of an array can be accessed by their position, called the index. In Java, array indexing starts from 0 (not 1). To access an element, provide the index inside square brackets [] along with the array name.
Published   May 8, 2026
Discussions

initializing arrays - Processing 2.x and 3.x Forum
Processing is an electronic sketchbook, a language and a worldwide community. This is its forum. More on forum.processing.org
🌐 forum.processing.org
February 28, 2014
Phil the Java Noob - Help on declaring/initializing arrays
The first step in your solution should be to declare and create an array that can hold three digits/integers, which would look something like: int[] pi = new int[3]; That statement declares an a integer array variable named pi, and assigns a new array (with a length of three) to that variable. An integer array is initially filled with all zeros, so you've got to fill it with the values that you want. In other words, you need to put 3 in the first index (index 0), 1 in the second index, and 4 in the third index. This should look something like: pi[0] = 3; // assign 3 to first index pi[1] = 1; pi[2] = 4; Now that the array is filled, your method should return the array, so that it can be used by whoever calls your makePi() method: return pi; There are several ways to declare an initialize arrays. A more concise way of doing it would be: int[] pi = {3, 1, 4}; or int[] pi = new int[]{3, 1, 4}; More on reddit.com
🌐 r/javahelp
9
3
July 10, 2018
How to initialize all elements in an array to 0
https://www.techiedelight.com/initialize-elements-array-same-value-c-cpp/ His examples you have to declare size of array. Same with the stackoverflow examples I just googled. More on reddit.com
🌐 r/cs50
3
1
August 27, 2020
[Java] Question about initializing all elements in an array to a specific given number.
In Java that array is on the heap, so if you change its elements in the function you’ll change it everywhere. No need to return anything, void is fine More on reddit.com
🌐 r/learnprogramming
4
1
March 4, 2021
🌐
HappyCoders.eu
happycoders.eu › java › initialize-array-java
How to Initialize Arrays in Java
June 12, 2025 - The following code creates an int array with the values 1 to 10, a long array with a sequence of numbers that starts at 1 and doubles as long as the value remains below 2,000, and a double array with a sequence of numbers that starts at 1.0 and is halved until a total of eight elements have been created:
🌐
Pluralsight
pluralsight.com › blog › software development
How to initialize an array in Java | Pluralsight
When you make an array in Java for the first time, you’ve got to declare its size. This is because the array’s size is fixed from the moment you create it. The simplest way to do this is to initialize an empty array of a declared size. Here’s an example: ... In the above case, we know that we will need 30 slots for data, one slot for each day in April. Right now, though, the array is just a container filled with empty slots waiting to be filled.
🌐
Sentry
sentry.io › sentry answers › java › how to initialize an array in java?
How to initialize an array in Java? | Sentry
This example is functionally equivalent to the previous example that declared and initialized our array separately. Interestingly, the square brackets that symbolize an array [] can be put on either side of the array name during the declaration with no change in functionality:
🌐
Techie Delight
techiedelight.com › home › java › declare and initialize arrays in java
Declare and initialize arrays in Java | Techie Delight
1 week ago - For example, the following code creates a primitive integer array of size 5. The array will be auto-initialized with a default value of 0. We can use the Arrays.fill() method to assign a specified value to each element of the specified array.
Find elsewhere
🌐
Javadevnotes
javadevnotes.com › java-initialize-array-examples
Java Initialize Array Examples - JavaDevNotes
December 25, 2015 - We may use for loops to initialize individual items of an array. Below is a simple example of how to assign the numbers 1 to 10 to an array with 10 values. import java.util.Arrays; /** * A Simple Example that Initialise A Java Array With Values 1 to 10.
🌐
Career Karma
careerkarma.com › blog › java › java initialize array: a step-by-step guide
Java Initialize Array: A Step-By-Step Guide | Career Karma
December 1, 2023 - Then we declare and initialize an array called bagelFlavors which stores the list of bagel flavors sold at our local bakery. Then we print out the value with the index number 1 in the bagelFlavors array. Our code returns the item at the index value 1, which is as follows: ... In the same way, we could access the element at index 0 to get ‘Plain,’ or the element at index 3 and get ‘Sesame.’ · In Java...
🌐
DataCamp
datacamp.com › doc › java › defining-and-initializing-arrays
Java Defining and Initializing Arrays
There are several ways to initialize an array in Java. You can initialize an array at the time of declaration using curly braces {} with the values separated by commas. dataType[] arrayName = {value1, value2, ..., valueN}; int[] numbers = {1, 2, 3, 4, 5}; String[] names = {"Alice", "Bob", ...
🌐
Medium
medium.com › @pies052022 › how-to-initialize-an-array-in-java-with-example-7b184ab0a983
How to Initialize an Array in Java with Example | by JOKEN VILLANUEVA | Medium
November 27, 2025 - Let’s look at the complete example code with an output result. package com.kolade; import java.util.Arrays; public class Main { public static void main(String[] args) { // write your code here String [] country = new String[3]; country[0] = "Philippines"; country[1] = "China"; country[2] = "Russia"; System.out.println(Arrays.toString(country)); } }
🌐
freeCodeCamp
freecodecamp.org › news › java-array-declaration-how-to-initialize-an-array-in-java-with-example-code
Java Array Declaration – How to Initialize an Array in Java with Example Code
September 9, 2021 - There are two ways you can declare and initialize an array in Java. The first is with the new keyword, where you have to initialize the values one by one.
🌐
Javatpoint
javatpoint.com › java-initialize-array
Java Initialize array - Javatpoint
March 15, 2013 - Java Initialize array with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Processing Forum
forum.processing.org › two › discussion › 3358 › initializing-arrays.html
initializing arrays - Processing 2.x and 3.x Forum
February 28, 2014 - ok i see so we are declaring and initializing the array itself at the class level as one object and instantiating our 20 other objects that make up the array in setup. ... That's right! 1st the structure, then its elements. :D Only a little extra curiosity: Arrays in Java aren't a class!
🌐
How to do in Java
howtodoinjava.com › home › java array › how to declare and initialize an array in java
How to Declare and Initialize an Array in Java
September 6, 2023 - The fill() method takes a value and assigns the specified value to each element of the specified array. In the given example, we are filling all the array elements with 1.
🌐
Software Testing Help
softwaretestinghelp.com › home › java tutorial for beginners: 100+ hands-on java video tutorials › java array – declare, create & initialize an array in java
Java Array - Declare, Create & Initialize An Array In Java
April 1, 2025 - Each element ‘i’ of the array is initialized with value = i+1. Apart from using the above method to initialize arrays, you can also make use of some of the methods of ‘Arrays’ class of ‘java.util’ package to provide initial values ...
🌐
TutorialsPoint
tutorialspoint.com › javaexamples › array_fill.htm
How to fill (initialize at once) an array in Java
This example fill (initialize all the elements of the array in one short) an array by using Array.fill(arrayname,value) method and Array.fill(arrayname ,starting index ,ending index ,value) method of Java Util class.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › arrays.html
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
// create an array of integers ... anArray may not have been initialized. The next few lines assign values to each element of the array: anArray[0] = 100; // initialize first element anArray[1] = 200; // initialize second element anArray[2] = 300; // and so forth ...
🌐
Reddit
reddit.com › r/javahelp › phil the java noob - help on declaring/initializing arrays
r/javahelp on Reddit: Phil the Java Noob - Help on declaring/initializing arrays
July 10, 2018 -

Me yet again! Arrays are really kicking my ass haha, can't seem to grasp them all that well.

Have a (seemingly) very basic question in regards to arrays:

" Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}."

What I have is this:

"public int[] makePi() {

int [3] arr;

int [1] arr;

int [4] arr;

}"

Can one of you kind souls explain to me what place(s) I'm going wrong? MOREOVER, can someone give me a brief rundown on how one declares arrays? Admittedly, my resources that I'm learning from aren't the best at explaining some concepts.

As always, thank you very much!!

🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-declare-and-initialize-an-array-in-java
How to Declare and Initialize an Array in Java - GeeksforGeeks
October 11, 2025 - int[] arr = new int[5]; for (int i = 0; i < arr.length; i++) { arr[i] = i + 1; // Fills array with 1, 2, 3, 4, 5 } Java 8 introduced IntStream to initialize integer arrays efficiently.