Home  >  Article  >  Java  >  What are the common ways to add elements to Java arrays?

What are the common ways to add elements to Java arrays?

王林
王林Original
2024-01-03 19:19:091178browse

What are the common ways to add elements to Java arrays?

Java array is a commonly used data structure used to store multiple elements of the same type. In actual development, it is often necessary to add elements to an array. This article will introduce common methods of adding array elements in Java and provide specific code examples.

1. Expanding arrays
Expanding arrays is one of the most common methods of adding elements to an array. When the array capacity is not enough to store new elements, you need to create a larger array and copy the elements from the old array to the new array.

Code example:

public static int[] expandArray(int[] arr, int size) {
    int[] newArr = new int[arr.length + size];
    System.arraycopy(arr, 0, newArr, 0, arr.length);
    return newArr;
}

Use the System.arraycopy() method to copy the elements in the old array to the new array, and then return the new array.

2. Use the ArrayList class
ArrayList is a class in the Java collection framework. It implements the function of a dynamic array and can automatically expand as needed. Elements can be added to an array by calling the add() method of the ArrayList class.

Code example:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list);  // 输出:[1, 2, 3]
    }
}

In the example, an ArrayList object list is created and three elements are added to the array by calling the add() method.

3. Use the Arrays.copyOf() method
The copyOf() method in the Arrays class can be used to copy an array and specify the length of the new array. Elements can be added to a new array by copying the array.

Code example:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        int[] newArr = Arrays.copyOf(arr, arr.length + 1);
        newArr[newArr.length - 1] = 4;
        System.out.println(Arrays.toString(newArr));  // 输出:[1, 2, 3, 4]
    }
}

In the example, the original array arr is copied by calling the Arrays.copyOf() method, and the length of the new array is specified to be the length of the original array plus 1, and then The element to be added is assigned to the last position of the new array.

Summary:
This article introduces three commonly used methods of adding elements to Java arrays and provides detailed code examples. Expanding arrays, using the ArrayList class and using the Arrays.copyOf() method are common methods in development. Choose the appropriate method to add array elements based on actual needs.

The above is the detailed content of What are the common ways to add elements to Java arrays?. 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