How to batch initialize arrays in Java that I didn't know when I was a beginner

Batch initialization of array

How to initialize all the elements of an array with 0 (Something like memset in C)

ʻArrays.fill (array, initialization value)`

import java.util.Arrays;
public class HelloWorld {
    public static void main (String[] args) {
        int[] intArray = new int[10];
        Arrays.fill(intArray, 0); //Specify the initialization value with the second argument
        System.out.println(Arrays.toString(intArray));
    }
}

// => [0, 0, 0, ...]

This method can also handle strings

import java.util.Arrays;
public class HelloWorld {
    public static void main (String[] args) {
        String[] str = new String[5];
        Arrays.fill(str, "_"); //Specify the initialization value with the second argument
        System.out.println(Arrays.toString(str));
    }
}

// => ["_", "_", "_", ...]

reference

Recommended Posts

How to batch initialize arrays in Java that I didn't know when I was a beginner
A memo that I was addicted to when making batch processing with Spring Boot
A site that was easy to understand when I was a beginner when I started learning Spring Boot
[Beginner] I made a program to sell cakes in Java
How to implement a job that uses Java API in JobScheduler
How to test a private method in Java and partially mock that method
How to pass a proxy when throwing REST over SSL in Java
How to display a web page in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I didn't know that inner classes could be defined in the [Java] interface
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
How to create a Java environment in just 3 seconds
I tried to create a Clova skill in Java
How to create a data URI (base64) in Java
I tried to make a login function in Java
What I learned when building a server in Java
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
A story I was addicted to when getting a key that was automatically tried on MyBatis
Java: A story that made me feel uncomfortable when I was taught to compare strings with equals for no reason.
A memorandum when I investigated how to convert from Epoch Time to Date type in Scala
I just wanted to make a Reactive Property in Java
How to store a string from ArrayList to String in Java (Personal)
A story I was addicted to in Rails validation settings
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
How to dynamically switch JDK when building Java in Gradle
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
I tried to make a client of RESAS-API in Java
How to initialize Java array
How to make a key pair of ecdsa in a format that can be read by Java
[Rails] How to solve ActiveSupport :: MessageVerifier :: InvalidSignature that I was addicted to when introducing twitter login [ActiveStorage]
How to deal with the type that I thought about writing a Java program for 2 years
How to create a new Gradle + Java + Jar project in Intellij 2016.03
A note when I was addicted to converting Ubuntu on WSL1 to WSL2
How to automatically operate a screen created in Java on Windows
I wrote a Stalin sort that feels like a mess in Java
How to run a job with docker login in AWS batch
About the matter that I was addicted to how to use hashmap
How to solve the unknown error when using slf4j in Java
The operator that was born to be born, instanceof (Java) ~ How to use the instanceof operator ~
I created a PDF in Java.
How to make a Java container
[Java] How to create a folder
How to use classes in Java?
How to name variables in Java
[Processing × Java] How to use arrays
How to make a Java array
How arrays work in Java (illustration)
How to concatenate strings in java
How to solve the problem that it is not processed normally when nesting beans in Spring Batch
How do you write in Scala that was written in Java? (List → Map)
[Personal memo] How to interact with a random number generator in Java
A story I was addicted to when testing the API using MockMVC
My.cnf configuration problem that I was addicted to when I was touching MySQL 8.0 like 5.7
I was addicted to a simple test of Jedis (Java-> Redis library)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
How to find the total number of pages when paging in Java
How to get the value after "_" in Windows batch like Java -version
I want to get the IP address when connecting to Wi-Fi in Java