Java中的集合 add() 方法及示例
Java.util.Collection 接口的add(E element)用于将元素“元素”添加到此集合中。此方法返回一个布尔值,表示操作是否成功。如果添加了元素,则返回 true,否则返回 false。
句法:
Collection.add(E element)
参数:此方法接受要添加到此集合的 E 类型的强制参数元素。
返回值:描述操作成功的布尔值。如果添加了元素,则返回 true,否则返回 false。
异常:此方法抛出以下 5 个异常,如下所示:
- UnsupportedOperationException:如果此集合不支持添加操作
- ClassCastException:如果指定元素的类阻止它被添加到此集合中
- NullPointerException:如果指定元素为空且此集合不允许空元素
- IllegalArgumentException:如果元素的某些属性阻止它被添加到此集合中
- IllegalStateException:如果此时由于插入限制无法添加元素
现在我们将在不同的类上实现这个方法,因为它在涉及到Java编程时是一个非常重要和必不可少的方法,所以在这里我们将强调每个类如下:
- 链表类
- 数组队列
- ArrayList 类
- 抛出 NullPointerException
让我们通过干净的Java示例在上面列出的所有 4 个案例中实现 add() 方法,如下所示:
示例 1: LinkedList 类
Java
// Java code to illustrate boolean add() method
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String args[])
{
// creating an empty LinkedList
Collection list = new LinkedList();
// use add() method to add elements in the list
list.add("Geeks");
list.add("for");
list.add("Geeks");
// Output the present list
System.out.println("The list is: " + list);
// Adding new elements to the end
list.add("Last");
list.add("Element");
// printing the new list
System.out.println("The new List is: " + list);
}
}
Java
// Java code to illustrate add() method
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Collection de_que = new ArrayDeque();
// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
}
}
Java
// Java code to illustrate add() method
import java.io.*;
import java.util.*;
public class ArrayListDemo {
public static void main(String[] args)
{
// create an empty array list with an initial capacity
Collection arrlist = new ArrayList(5);
// use add() method to add elements in the list
arrlist.add(15);
arrlist.add(20);
arrlist.add(25);
// prints all the elements available in list
for (Integer number : arrlist) {
System.out.println("Number = " + number);
}
}
}
Java
// Java code to illustrate boolean add()
// Where NullPointerException is Thrown
// Importing required utility classes
import java.util.*;
// Main class
// LinkedListDemo
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty ArrayList of string type
Collection list = new ArrayList();
// Printing and displaying the Arraylist
System.out.println("The ArrayList is: " + list);
// Note: Here by now we have not added any element/s
// Try block to check for exceptions
try {
// Appending the null to the list
// using add() method
list.add(null);
}
// Catch block to handle exceptions
catch (Exception e) {
// Display message when exceptions occurs
System.out.println("Exception: " + e);
}
}
}
输出:
The list is: [Geeks, for, Geeks]
The new List is: [Geeks, for, Geeks, Last, Element]
示例 2: ArrayDeque 类
Java
// Java code to illustrate add() method
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Collection de_que = new ArrayDeque();
// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
}
}
输出:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
示例 3:使用 ArrayList 类
Java
// Java code to illustrate add() method
import java.io.*;
import java.util.*;
public class ArrayListDemo {
public static void main(String[] args)
{
// create an empty array list with an initial capacity
Collection arrlist = new ArrayList(5);
// use add() method to add elements in the list
arrlist.add(15);
arrlist.add(20);
arrlist.add(25);
// prints all the elements available in list
for (Integer number : arrlist) {
System.out.println("Number = " + number);
}
}
}
输出:
Number = 15
Number = 20
Number = 25
Geeks do keep an bound over special case where NullPointer Exception will be thrown as show in below example as follows:
示例 4:
Java
// Java code to illustrate boolean add()
// Where NullPointerException is Thrown
// Importing required utility classes
import java.util.*;
// Main class
// LinkedListDemo
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty ArrayList of string type
Collection list = new ArrayList();
// Printing and displaying the Arraylist
System.out.println("The ArrayList is: " + list);
// Note: Here by now we have not added any element/s
// Try block to check for exceptions
try {
// Appending the null to the list
// using add() method
list.add(null);
}
// Catch block to handle exceptions
catch (Exception e) {
// Display message when exceptions occurs
System.out.println("Exception: " + e);
}
}
}
输出:
The ArrayList is: []
输出说明:这里我们需要把它捡起来,因为我们只会收到一个列表。因此,记录 add() 方法是否接受它是否需要支持 null 是一种好习惯。