Home > Java > javaTutorial > body text

Java copies arrays using copyOf() function of Arrays class

WBOY
Release: 2023-07-24 15:39:16
Original
2179 people have browsed it

Java uses the copyOf() function of the Arrays class to copy an array

In Java, we often need to copy an array in order to operate or create a new copy without changing the original array. The Arrays class is a utility class provided by Java, which contains various static methods for operating arrays. The copyOf() function is used to copy arrays.

The syntax of the copyOf() function is as follows:

public static T[] copyOf(T[] original, int newLength)

The function of this function is to copy the original array to original Copy the first newLength elements into a new array and return this new array. During the copying process, if the length of the original array is less than newLength, the length of the new array will be the same as the length of the original array.

Next, let us use an example to demonstrate how to copy an array using the copyOf() function.

import java.util.Arrays;

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

        // 复制数组
        int[] copiedArray = Arrays.copyOf(originalArray, originalArray.length);

        // 打印原始数组和复制数组
        System.out.println("原始数组:");
        for (int i = 0; i < originalArray.length; i++) {
            System.out.print(originalArray[i] + " ");
        }

        System.out.println("
复制数组:");
        for (int i = 0; i < copiedArray.length; i++) {
            System.out.print(copiedArray[i] + " ");
        }
    }
}
Copy after login

In the above example, we first created an original array originalArray, containing 5 integer elements. Then, copy all elements of the original array to a new array copiedArray by calling the Arrays.copyOf() function. Finally, we print out all elements of the original array and the copied array respectively.

Run the above code, the output is as follows:

原始数组:
1 2 3 4 5 
复制数组:
1 2 3 4 5 
Copy after login

As you can see from the output, all elements of the copied array and the original array are the same. This is also the basic idea of ​​using the Arrays.copyOf() function to copy an array.

It should be noted that when copying an array, you can specify the length of the new array. If the length of the new array is greater than the length of the original array, the new array will be padded with default values. For example, if the element type of the original array is int, the remaining positions will be filled with 0s.

int[] copiedArray = Arrays.copyOf(originalArray, 7);

// 输出:1 2 3 4 5 0 0
Copy after login

In addition, the copyOf() function can also be used to copy arrays of non-basic types, such as string arrays, object arrays, etc.

Summary:
In Java, arrays can be easily copied using the copyOf() function of the Arrays class. By specifying the lengths of the original array and the new array, we can quickly get a new copy of the array. In this way, we can modify or operate the copy array while keeping the original array unchanged.

The above is the detailed content of Java copies arrays using copyOf() function of Arrays class. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!