Home > Java > javaTutorial > Java uses the set() function of the ArrayList class to modify the elements in the collection

Java uses the set() function of the ArrayList class to modify the elements in the collection

WBOY
Release: 2023-07-25 18:51:18
Original
2292 people have browsed it

Java uses the set() function of the ArrayList class to modify the elements in the collection

ArrayList is one of the commonly used collection classes in Java. It implements the List interface and provides a way to implement dynamic arrays. The ArrayList class provides a variety of methods to operate elements in the collection, among which the set() function is a commonly used method, which can be used to modify the values ​​of elements in the collection.

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

boolean set(int index, E element)
Copy after login

Among them, index represents the index of the element to be modified, and element represents the new value to be modified. This function returns the old value before modification.

Below we use an example to explain in detail how to use the set() function to modify the elements in the ArrayList collection.

First, introduce the ArrayList class into the code:

import java.util.ArrayList;
Copy after login

Then, create an ArrayList object:

ArrayList<String> list = new ArrayList<String>();
Copy after login

Next, add some elements to the collection:

list.add("Apple");
list.add("Banana");
list.add("Orange");
Copy after login

Now, we want to change the second element in the collection "Banana" to "Grape". You can use the set() function to achieve this:

String oldValue = list.set(1, "Grape");
Copy after login

In the above code, the set(1, "Grape") function is called to modify the element with index 1 in the collection to "Grape". At the same time, the set() function returns the old value "Banana" before modification, which we can assign to the oldValue variable.

Next, we can print out the modified collection elements and old values:

System.out.println("修改后的集合元素:" + list);
System.out.println("修改前的旧值:" + oldValue);
Copy after login

The complete sample code is as follows:

import java.util.ArrayList;

public class ArrayListSetExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();

        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String oldValue = list.set(1, "Grape");

        System.out.println("修改后的集合元素:" + list);
        System.out.println("修改前的旧值:" + oldValue);
    }
}
Copy after login

Run the above code, the output result is:

修改后的集合元素:[Apple, Grape, Orange]
修改前的旧值:Banana
Copy after login

It can be seen from the output that the set() function successfully modified the element "Banana" in the collection to "Grape" and returned the old value "Banana".

Summary:
Through the set() function of the ArrayList class, we can easily modify the elements in the collection. You only need to pass in the index of the element to be modified and the new value to complete the modification operation. In addition, the set() function also returns the old value before modification, which is convenient for us to do other operations. Therefore, the set() function is a very useful and practical method when using the ArrayList class.

The above is the detailed content of Java uses the set() function of the ArrayList class to modify the elements in the collection. 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