Home > Java > javaTutorial > Use java's Arrays.fill() function to fill all elements in the array to the specified value

Use java's Arrays.fill() function to fill all elements in the array to the specified value

WBOY
Release: 2023-07-25 13:53:10
Original
1564 people have browsed it

Use java's Arrays.fill() function to fill all the elements in the array with the specified value

In Java programming, it is often necessary to fill all the elements in the array with the specified value. In order to facilitate this function, Java provides a very convenient function-Arrays.fill().

The Arrays.fill() function is a static method of the Arrays class in Java, used to fill all elements in the array with specified values. Using this method is very simple, just pass in the array to be filled and the specified value. The following is a sample code:

import java.util.Arrays;

public class FillArrayExample {
    public static void main(String[] args) {
        // 创建一个包含10个元素的整型数组,并将所有元素填充为0
        int[] arr = new int[10];
        Arrays.fill(arr, 0);
        
        // 打印数组元素
        System.out.println("填充后的数组元素:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
Copy after login

In the above code, we first create an integer array arr containing 10 elements. Then, all elements in array arr are filled with 0 by calling the Arrays.fill() function.

The filled array elements are as follows:

填充后的数组元素:
0 0 0 0 0 0 0 0 0 0
Copy after login

It should be noted that the Arrays.fill() function will fill all elements of the entire array with the specified value, so when using this When using the function, make sure that the length of the array is consistent with the number of filled elements.

In addition to filling integer arrays, the Arrays.fill() function can also be used to fill arrays of other basic data types, such as byte, short, long, float, double, char, etc. At the same time, this function can also be used to fill arrays of reference types, such as String, Object, etc.

The following is a sample code to demonstrate how to use the Arrays.fill() function to fill a String type array:

import java.util.Arrays;

public class FillArrayExample {
    public static void main(String[] args) {
        // 创建一个包含5个元素的String类型数组,并将所有元素填充为"Hello"
        String[] arr = new String[5];
        Arrays.fill(arr, "Hello");
        
        // 打印数组元素
        System.out.println("填充后的数组元素:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
Copy after login

In the above code, we created a String type containing 5 elements Array arr and fill all elements with "Hello". The filled array elements are as follows:

填充后的数组元素:
Hello Hello Hello Hello Hello
Copy after login

Summary:

Using Java's Arrays.fill() function can easily fill all the elements in the array with the specified value. This function works on arrays of basic data types and reference types. During use, you need to pay attention to ensure that the length of the array is consistent with the number of filled elements.

Hope this article can help you understand and use the Arrays.fill() function.

The above is the detailed content of Use java's Arrays.fill() function to fill all elements in the array to the specified value. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template