Home > Java > javaTutorial > body text

How to add elements in Java using add() method of ArrayList class

PHPz
Release: 2023-07-24 19:54:26
Original
1900 people have browsed it

How to add elements in Java using the add() method of the ArrayList class

ArrayList is one of the common collection classes in Java. It provides convenient methods to manage dynamic length arrays. Adding elements to an ArrayList is one of the common operations, and the add() method is one of the main methods to implement this operation.

The use of the add() method is very simple, it can add an element to the end of the ArrayList. Below is a sample code that demonstrates how to add elements to an ArrayList using the add() method.

import java.util.ArrayList;

public class AddElementExample {
   public static void main(String[] args) {
      // 创建一个ArrayList对象
      ArrayList<String> fruits = new ArrayList<>();

      // 使用add()方法添加元素
      fruits.add("苹果");
      fruits.add("香蕉");
      fruits.add("葡萄");

      // 打印ArrayList中的元素
      System.out.println("水果列表:" + fruits);
   }
}
Copy after login

In this sample code, we first imported the java.util.ArrayList class, and then created an ArrayList object named fruits.

Next, we use the add() method to add three elements to the ArrayList, namely "Apple", "Banana" and "Grape". When calling the add() method, we pass the element to be added as a parameter to the method. Since ArrayList is a dynamic array, we don't need to worry about the length of the array and can add new elements at any time.

Finally, we use the println() method to print out the elements in the ArrayList to verify the results of the addition operation.

As you can see, the output result is Fruit list: [Apple, Banana, Grape], indicating that we have successfully added three elements to the ArrayList.

It should be noted that the add() method adds elements to the end of the ArrayList, which means that the newly added element will become the last element in the ArrayList. If we want to add an element at a specified position, we can use the add(index, element) method, where index is the index value of the specified position and element is to be added Elements.

To summarize, using the add() method of the ArrayList class is a common way to add elements in Java. It is simple and easy to use, just pass the element to be added as a parameter to the add() method. At the same time, the ArrayList class provides many other useful methods, which can easily add, delete, modify, and query dynamic arrays, making data management more flexible and efficient.

The above is the detailed content of How to add elements in Java using add() method of ArrayList 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