Thursday, January 23, 2025
HomeProgrammingJava Arrays.fill()

Java Arrays.fill()

In Java, arrays are one of the most commonly used data structures. They allow you to store multiple values of the same type in a single variable, making it easy to work with collections of data. One of the useful methods in Java when dealing with arrays is Arrays.fill(). This method allows you to fill an entire array, or a portion of it, with a specific value in a quick and efficient way.

In this blog post, we’ll take a deep dive into the Arrays.fill() method, how it works, and show you some practical examples to help you understand how to use it effectively.

What is Arrays.fill() in Java?

The Arrays.fill() method is part of the java.util.Arrays class and is used to assign a specific value to every element in an array. You can use it to fill the entire array or just a specific range of indices within the array. This method is particularly useful when you need to initialize an array with a specific value or reset an array’s values to a default one.

Syntax:

Arrays.fill(array, value);
  • array: The array you want to fill with values.
  • value: The value you want to assign to each element of the array.

Different Variants of Arrays.fill()

The Arrays.fill() method has several overloaded versions that allow you to specify different ranges and types of arrays. Here are the main variants:

  1. Filling the Entire Array: You can use the Arrays.fill() method to fill all elements of an array with the same value.
    Arrays.fill(array, value);
    
  2. Filling a Range of the Array: If you want to fill a specific range of an array, you can provide the starting index and the ending index (exclusive) in addition to the value.
    Arrays.fill(array, fromIndex, toIndex, value);
    
    • fromIndex: The index to start filling from (inclusive).
    • toIndex: The index to stop filling at (exclusive).
See also  What is Bellman-Ford algorithm And How Does it Works?

Examples of Using Arrays.fill()

Let’s go through some examples to see how Arrays.fill() can be used effectively.

Example 1: Filling an Entire Array

In this example, we will create an integer array and use Arrays.fill() to fill all elements with the value 5.

import java.util.Arrays;

public class ArraysFillExample {
    public static void main(String[] args) {
        int[] numbers = new int[10]; // Creates an array of 10 elements.
        
        // Fill the entire array with the value 5.
        Arrays.fill(numbers, 5);
        
        // Print the array to verify that all elements are 5.
        System.out.println(Arrays.toString(numbers));
    }
}

Output:

[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

In this case, Arrays.fill(numbers, 5) fills every index of the numbers array with the value 5.

Example 2: Filling a Specific Range of an Array

Sometimes, you only need to fill a specific range of elements in an array. In this example, we will fill elements from index 3 to index 6 with the value 8.

import java.util.Arrays;

public class ArraysFillExample {
    public static void main(String[] args) {
        int[] numbers = new int[10]; // Creates an array of 10 elements.
        
        // Fill indices 3 to 6 (exclusive) with the value 8.
        Arrays.fill(numbers, 3, 7, 8);
        
        // Print the array to see the filled values.
        System.out.println(Arrays.toString(numbers));
    }
}

Output:

[0, 0, 0, 8, 8, 8, 8, 0, 0, 0]

In this case, Arrays.fill(numbers, 3, 7, 8) fills the portion of the array from index 3 to 6 (index 7 is exclusive) with the value 8. The rest of the array remains unchanged.

See also  Windows - How to do a Simple File Search in cmd

Example 3: Filling an Array of Objects

The Arrays.fill() method can also be used with arrays of objects. Here’s an example using a String array, where all elements are filled with a default value.

import java.util.Arrays;

public class ArraysFillExample {
    public static void main(String[] args) {
        String[] names = new String[5]; // Creates an array of 5 elements.
        
        // Fill the entire array with the value "John".
        Arrays.fill(names, "John");
        
        // Print the array to verify the result.
        System.out.println(Arrays.toString(names));
    }
}

Output:

[John, John, John, John, John]

In this case, every element of the names array is filled with the string "John".

Why Use Arrays.fill()?

The Arrays.fill() method offers several advantages:

  1. Efficiency: Instead of using a loop to assign values to an array, Arrays.fill() performs the operation in a more concise and optimized manner.
  2. Readability: The code becomes cleaner and more readable when using Arrays.fill() as opposed to writing multiple lines of code for looping through the array.
  3. Flexibility: You can choose to fill the entire array or a specific range of elements, giving you flexibility in how you initialize or reset your array.
  4. Simplifies Initialization: It’s commonly used for initializing arrays with default values or resetting arrays during the lifecycle of a program.
See also  How can I generate a list of files with their absolute path in a directory using a command-line tool?

Limitations of Arrays.fill()

  • Primitive Arrays Only: The Arrays.fill() method works with arrays of primitive types, such as int[], char[], etc., as well as arrays of objects. However, when working with object arrays, be mindful that it assigns the reference of the object rather than copying a deep copy of the object.
  • Does Not Handle Multi-Dimensional Arrays: For multi-dimensional arrays, Arrays.fill() fills the array with references, meaning each row in a 2D array will point to the same reference.

Conclusion

The Arrays.fill() method in Java is a powerful and convenient tool for filling arrays with specific values. Whether you are initializing an array with default values or resetting a range of elements, Arrays.fill() simplifies the process and makes your code more concise and efficient. With its flexible options for filling entire arrays or partial ranges, it is a method you’ll likely use often in your Java development.

We hope this post has helped you understand how to use Arrays.fill() in your Java programs. Happy coding!

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x