Home  >  Article  >  Java  >  Use the copyOfRange() method of the Arrays class in Java to copy part of an array

Use the copyOfRange() method of the Arrays class in Java to copy part of an array

王林
王林Original
2023-07-25 17:21:231831browse

Use the copyOfRange() method of the Arrays class in Java to copy part of the contents of an array

In Java, to copy part of the contents of an array, you can use the copyOfRange() method of the Arrays class. This method copies the specified range of elements in the source array and returns a new array.

The syntax of the copyOfRange() method is as follows:

public static 8742468051c85b06f0a0af9e3e506b5c T[] copyOfRange(T[] original, int from, int to)

original: The source array to be copied
from: the index where the copy starts (inclusive)
to: the index where the copy ends (exclusive)
Return value: the new array after copying

Example below Demonstrates how to use the copyOfRange() method to copy part of an array:

import java.util.Arrays;

public class ArrayCopyExample {
    public static void main(String[] args) {
        // 原始数组
        Integer[] originalArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        // 复制原始数组的一部分内容
        Integer[] copiedArray = Arrays.copyOfRange(originalArray, 2, 7);

        // 输出原始数组
        System.out.println("原始数组为:");
        System.out.println(Arrays.toString(originalArray));

        // 输出复制后数组
        System.out.println("复制后的数组为:");
        System.out.println(Arrays.toString(copiedArray));
    }
}

The output is as follows:

原始数组为:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
复制后的数组为:
[3, 4, 5, 6, 7]

In the above example, we define an original array originalArray and use Arrays. The copyOfRange() method copies a portion of the original array. Starts at index 2 (inclusive of 2) and ends at index 7 (exclusive of 7). The result is a new array copiedArray containing elements from index 2 to index 6.

By calling the Arrays.toString() method, we can convert the array to a string and use the System.out.println() method to print the array. As can be seen from the output, the elements in the original array are [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], while the elements in the copied array are [3, 4, 5 , 6, 7].

The copyOfRange() method is very convenient to copy part of the contents of an array without having to manually create a new array and copy elements one by one. This is very helpful when working with large arrays or when you need to operate on part of an array.

It should be noted that if the specified range exceeds the boundaries of the original array, an ArrayIndexOutOfBoundsException exception will be thrown. When using the copyOfRange() method, make sure the range is correct.

In summary, the Arrays class in Java provides a copyOfRange() method for copying part of the array. By specifying the starting index and the ending index, you can easily copy the required elements and return a new array. This approach can greatly simplify the work of array copying and improve the readability of the code.

The above is the detailed content of Use the copyOfRange() method of the Arrays class in Java to copy part of an array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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