Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. Arrays.copyOf() / Arrays.copyOfRange() / Arrays.fill()

Arrays.copyOf() / Arrays.copyOfRange() / Arrays.fill()

These methods copy an array to create a new one, or fill all or a specified range of elements with a given value. They are provided as static methods of the java.util.Arrays class.

Syntax

import java.util.Arrays;

// Copies an array and returns a new array of the specified length.
Arrays.copyOf(original, int newLength);

// Copies a specified range and returns a new array.
Arrays.copyOfRange(original, int from, int to);

// Fills the entire array with the specified value (destructive operation).
Arrays.fill(array, value);

// Fills a specified range with the specified value (destructive operation).
Arrays.fill(array, int fromIndex, int toIndex, value);

// Performs a fast array copy.
System.arraycopy(src, int srcPos, dest, int destPos, int length);

Method List

MethodDescription
Arrays.copyOf(original, newLength)Copies an array. If newLength is greater than the original length, the extra elements are filled with default values (0, null, etc.).
Arrays.copyOfRange(original, from, to)Returns a new array containing elements from from up to (but not including) to.
Arrays.fill(array, value)Overwrites every element of the array with value. Modifies the original array directly.
Arrays.fill(array, fromIndex, toIndex, value)Overwrites elements from fromIndex up to (but not including) toIndex with value.
System.arraycopy(src, srcPos, dest, destPos, length)Performs a fast array copy using a low-level native implementation. Well-suited for copying large amounts of data.

Sample Code

import java.util.Arrays;

// Copy an array using Arrays.copyOf().
int[] original = {1, 2, 3, 4, 5};
int[] copy = Arrays.copyOf(original, 3); // Copies the first 3 elements.
System.out.println(Arrays.toString(copy)); // Outputs "[1, 2, 3]".

// Copy with a larger length (extra elements are filled with 0).
int[] extended = Arrays.copyOf(original, 8);
System.out.println(Arrays.toString(extended)); // Outputs "[1, 2, 3, 4, 5, 0, 0, 0]".

// Copy a range using Arrays.copyOfRange().
int[] range = Arrays.copyOfRange(original, 1, 4); // Copies indexes 1 through 3.
System.out.println(Arrays.toString(range)); // Outputs "[2, 3, 4]".

// Fill all elements using Arrays.fill().
int[] filled = new int[5];
Arrays.fill(filled, 7);
System.out.println(Arrays.toString(filled)); // Outputs "[7, 7, 7, 7, 7]".

// Fill a specific range.
int[] partial = {0, 0, 0, 0, 0};
Arrays.fill(partial, 1, 4, 9); // Fills indexes 1 through 3 with 9.
System.out.println(Arrays.toString(partial)); // Outputs "[0, 9, 9, 9, 0]".

// Perform a fast copy using System.arraycopy().
int[] src = {10, 20, 30, 40, 50};
int[] dest = new int[5];
System.arraycopy(src, 1, dest, 0, 3); // Copies elements 1–3 of src to the start of dest.
System.out.println(Arrays.toString(dest)); // Outputs "[20, 30, 40, 0, 0]".

Notes

Arrays.copyOf() and Arrays.copyOfRange() return a new array and do not affect the original. You can also use them to resize an array. When copying an array of objects, only the references are copied (shallow copy). Modifying an object through the copy will also affect the original array, so implement a deep copy if you need a fully independent copy.

Arrays.fill() is commonly used to initialize arrays. It is useful whenever you want to set all elements to the same value, such as when initializing a game map or setting up default values.

To sort an array, use Arrays.sort(). To compare or convert arrays to strings, use Arrays.equals() / Arrays.toString().

If you find any errors or copyright issues, please .