Home > Java > javaTutorial > What is the difference between the set method and the add method in List in java?

What is the difference between the set method and the add method in List in java?

王林
Release: 2023-04-19 19:49:29
forward
1486 people have browsed it

Preface

There are two very similar methods in the commonly used collection interface List in Java:

E set(int index, E element);

void add(int index, E element);

Both of these two methods insert the specified element at the specified position in the collection, so what are the two methods? what differences are there? Next, let’s take a look at the differences between these two methods through ArrayList, our commonly used collection implementation.

Similarities

First, let’s take a look at the similarities between these two methods in ArrayList

They will all insert new elements at the specified position in the collection, such as the following example:

#在集合的第2位插入一个 F
# 通过add方法插入
List<String> list = new ArrayList<String>();
        list.add("A");
        list.add("B");
        list.add("C");
        System.out.println(list);

        list.add(2, "F");
        System.out.println(list);
        
#运行结果:  [A, B, F, C]

#通过set方法插入
List<String> list = new ArrayList<String>();
        list.add("A");
        list.add("B");
        list.add("C");
        System.out.println(list);

        list.set(2, "F");
        System.out.println(list);
#运行结果: [A, B, F]
Copy after login
Copy after login
  • The element in the above two methods is not allowed to be null, if it is null If so, a NullPointerException will be thrown

  • If the class corresponding to the element in the above two methods cannot be stored in the collection, a ClassCastException will be thrown

  • If the type corresponding to the element in the above two methods cannot be stored in the collection, an IllegalArgumentException will be thrown

Differences

The first difference is from above It can be seen from the example that the add method inserts a new value at the specified position, and then moves the element
at the original position and all elements after the original position one position backward, while the set method directly overwrites the original The value range of the element

#在集合的第2位插入一个 F
# 通过add方法插入
List<String> list = new ArrayList<String>();
        list.add("A");
        list.add("B");
        list.add("C");
        System.out.println(list);

        list.add(2, "F");
        System.out.println(list);
        
#运行结果:  [A, B, F, C]

#通过set方法插入
List<String> list = new ArrayList<String>();
        list.add("A");
        list.add("B");
        list.add("C");
        System.out.println(list);

        list.set(2, "F");
        System.out.println(list);
#运行结果: [A, B, F]
Copy after login
Copy after login

add method for index is 0<= index <= size(), which means that the add method can add a new element after the last element of the current collection; set The value range of the method for index is 0< index < size(), which means that the set method can only cover the current existing elements, and the value range cannot exceed the range of the current collection elements

#通过add在当前集合的第四个位置插入F
 List<String> list = new ArrayList<String>();
        list.add("A");
        list.add("B");
        list.add("C");
        System.out.println(list);
        list.add(3, "F");
        System.out.println(list);
        
#运行结果: [A, B, C, F]

#通过set在当前集合的第四个位置插入F
 List<String> list = new ArrayList<String>();
        list.add("A");
        list.add("B");
        list.add("C");
        System.out.println(list);
        list.set(3, "F");
        System.out.println(list);
        
#运行结果: 抛出NullPointerException
Copy after login

The above is the detailed content of What is the difference between the set method and the add method in List in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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