集合类unmodifiableList()方法 (Collections Class unmodifiableList() method)
unmodifiableList() method is available in java.util package.
unmodifiableList()方法在java.util包中可用。
unmodifiableList() method is used to get a non-modifiable view of the given List (l).
unmodifiableList()方法用于获取给定列表(l)的不可修改视图。
unmodifiableList() method is a static method, it is accessible with the class name and if we try to access the method with the class object then also we will not get any error.
unmodifiableList()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,那么也不会出现任何错误。
unmodifiableList() method does not throw an exception at the time of returning an unmodifiable view of the given list.
返回给定列表的不可修改视图时, unmodifiableList()方法不会引发异常。
Syntax:
句法:
public static List unmodifiableList(List l) ;
Parameter(s):
参数:
List l – represents the list object for which a non-modifiable view is to be retrieved.
列表l –表示要为其检索不可修改视图的列表对象。
Return value:
返回值:
The return type of this method is List, it returns an unmodifiable view of the given list.
此方法的返回类型为List ,它返回给定列表的不可修改视图。
Example:
例:
// Java program to demonstrate the example
// of List unmodifiableList()
// method of Collections
import java.util.*;
public class UnmodifiableListOfCollections {
public static void main(String args[]) {
// Instantiates a linked list object
List ll = new LinkedList();
// By using add() method is to add
// objects in a linked list
ll.add(10);
ll.add(20);
ll.add(30);
ll.add(40);
ll.add(50);
// Display Linked list
System.out.println("LinkedList: " + ll);
// By using unmodifiableList() method is to
// represent the linked lis in an unmodifiable view
ll = Collections.unmodifiableList(ll);
// We will get an exception when we
// try to add an object in an unmodifiable
// linked list
/* ll.add(60);*/
}
}
Output
输出量
LinkedList: [10, 20, 30, 40, 50]
翻译自: https://www.includehelp.com/java/collections-unmodifiablelist-method-with-example.aspx