Home > Java > javaTutorial > How to add element to list in Java?

How to add element to list in Java?

PHPz
Release: 2023-09-11 23:49:02
forward
1963 people have browsed it

How to add element to list in Java?

We can add elements to the list using the add() method of List.

1. Use the add() method without index.

boolean add(E e)
Copy after login

Append the specified element to the end of this list (optional operation).

Parameters

  • e - The element to append to this list.

Returns

True (specified by Collection.add(E)).

Throws

  • UnsupportedOperationException - if this list does not support the add operation.

  • ClassCastException - if the specified element's class prevents it from being added to this list.

  • NullPointerException - If the specified element is null and this list does not allow null elements.

  • IllegalArgumentException - If some attribute of this element prevents it from being added to this list.

2. Use add() with the index parameter to add an element at a specific position.

void add(int index, E element)
Copy after login

Insert the specified element at the specified position in this list (optional operation). Moves the element currently at that position (if any) and all subsequent elements to the right (incrementing their index by one).

Parameters

  • < p>index - The index at which the specified element is to be inserted.

  • element - The element to insert.

Throws

  • UnsupportedOperationException - if the add operation is not supported

  • ClassCastException - if the specified element's class prevents it from being added to this list.

  • NullPointerException - if the specified element is null and this list does not allow null elements.

  • IllegalArgumentException< /strong> - if some properties of the element prevent it from being added to this list.

  • IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()).

Example

The following example shows the usage of add() method-

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;

public class CollectionsDemo {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(5);
      list.add(6);
      System.out.println("List: " + list);
      list.add(3, 4);
      System.out.println("List: " + list);
      try {
         list.add(7, 7);
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
      }
   }
}
Copy after login

Output

This will produce the following result-

List: [1, 2, 3, 5, 6]
List: [1, 2, 3, 4, 5, 6]
java.lang.IndexOutOfBoundsException: Index: 7, Size: 6
   at java.base/java.util.ArrayList.rangeCheckForAdd(ArrayList.java:788)
   at java.base/java.util.ArrayList.add(ArrayList.java:513)
   at com.tutorialspoint.CollectionsDemo.main(CollectionsDemo.java:22)
Copy after login

The above is the detailed content of How to add element to list in Java?. For more information, please follow other related articles on the PHP Chinese website!

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