Home  >  Article  >  Java  >  In Java, how to add new elements to an array?

In Java, how to add new elements to an array?

PHPz
PHPzOriginal
2024-01-03 15:30:431508browse

In Java, how to add new elements to an array?

Adding new elements to an array is a common operation in Java and can be implemented using a variety of methods. This article will introduce several common methods of adding elements to an array and provide corresponding code examples.

1. Use a new array

A common method is to create a new array, copy the elements of the original array to the new array, and add new elements at the end of the new array. The specific steps are as follows:

  1. Create a new array with a size 1 larger than the original array. This is because a new element is being added.
  2. Copy the elements of the original array to the new array.
  3. Add new elements to the end of the new array.

The following is a code example for adding new elements using a new array:

public class ArrayAddElementExample {
    public static void main(String[] args) {
        int[] originalArray = {1, 2, 3, 4, 5};
    
        // 创建一个新的数组,大小比原数组大1
        int[] newArray = new int[originalArray.length + 1];
    
        // 将原数组的元素复制到新数组中
        for (int i = 0; i < originalArray.length; i++) {
            newArray[i] = originalArray[i];
        }
    
        // 在新数组的末尾添加新元素
        int newElement = 6;
        newArray[newArray.length - 1] = newElement;
    
        // 输出新数组
        for (int i = 0; i < newArray.length; i++) {
            System.out.print(newArray[i] + " ");
        }
    }
}

The running result is: 1 2 3 4 5 6

2. Use the ArrayList class

There is also a convenient class ArrayList in Java that can dynamically add and delete elements. Adding new elements to an array is more convenient using the ArrayList class.

The following is a code example for adding new elements using the ArrayList class:

import java.util.ArrayList;

public class ArrayListAddElementExample {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
    
        // 添加新元素
        int newElement = 6;
        list.add(newElement);
    
        // 输出ArrayList
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
    }
}

The running results are: 1 2 3 4 5 6

Summary

Introduction to this article There are two common ways to add new elements to an array: using a new array and using the ArrayList class. Using a new array requires manually creating a new array, copying the elements of the original array, and adding new elements. However, using the ArrayList class makes it easier to dynamically add and delete elements. Choose the appropriate method to add new elements to the array according to the actual situation.

The above is the detailed content of In Java, how to add new elements to 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