使ArrayList只读 (Making ArrayList Read-Only)
Given an ArrayList, and we have to make it Read-Only in Java.
给定一个ArrayList,我们必须使其成为Java只读。
Read-Only: If we make ArrayList as Read-Only i.e. we can only read ArrayList and we cannot perform other operations on ArrayList like delete, replace, add by using remove(), set(), add() methods, in read-only mode or in other words we cannot perform any modification in the ArrayList during Read-Only mode.
只读:如果我们将ArrayList设为只读,即我们只能读取ArrayList,则无法在ArrayList上执行其他操作,如delete,replace,add(使用remove(),set(),add()方法),如read-只读模式,换句话说,我们不能在只读模式下对ArrayList进行任何修改。
To make an ArrayList read-only, we use unmodifiableCollection() method of the Collections class.
为了使ArrayList为只读,我们使用Collections类的unmodifiableCollection()方法 。
unmodifiableCollection() method
unmodifiableCollection()方法
unmodifiableCollection() method is available in java.util package.
unmodifiableCollection()方法在java.util包中可用。
unmodifiableCollection() method is used to make java collections (ArrayList) read-only.
unmodifiableCollection()方法用于将Java集合(ArrayList)设为只读。
unmodifiableCollection() method is used to returns the same ArrayList as we input (i.e. unmodifiable view).
unmodifiableCollection()方法用于返回与我们输入相同的ArrayList(即,不可修改的视图)。
unmodifiableCollection() method may throw an exception at the time of modification in unmodifiableCollection view.
在unmodifiableCollection视图中进行修改时, unmodifiableCollection()方法可能会引发异常。
UnsupportedOperationException: In this exception, if we attempt to modify the collection.
UnsupportedOperationException:在此异常中,如果我们尝试修改集合。
Syntax:
句法:
public static Collection unmodifiableCollection(Collection co){
}
Parameter(s):
参数:
co –represents the ArrayList collection object for which an unmodifiable view is to be returned.
共 -代表的ArrayList集合对象一个不可修改视图将被返回。
Return value:
返回值:
The return type of this method is Collection, it returns an unmodifiable view of the collection.
此方法的返回类型为Collection ,它返回该集合的不可修改视图。
Example:
例:
// Java program to demonstrate the example of
// Java ArrayList make Read-Only by using
// unmodifiableCollection() method of Collections class
import java.util.*;
public class ArrayListMakeReadOnly {
public static void main(String[] args) {
// ArrayList Declaration
Collection arr_list = new ArrayList();
// By using add() method to add few elements in
// ArrayList
arr_list.add(10);
arr_list.add(20);
arr_list.add(30);
arr_list.add(40);
arr_list.add(50);
// Display ArrayList
System.out.println("Display ArrayList Elements");
System.out.println(arr_list);
System.out.println();
// By using unmodifiableCollection() method is used to make
// ArrayList Read-Only
Collection al_ro = Collections.unmodifiableCollection(arr_list);
// We will get an exception if we add element in Read-Only
// ArrayList i.e. Below statement is invalid
// al_ro.add(60);
// We will get an exception if we delete element from Read-Only
// ArrayList i.e. Below statement is invalid
// al_ro.remove(1);
// We will get an exception if we replace element in Read-Only
// ArrayList i.e. Below statement is invalid
// al_ro.set(2,60);
}
}
Output
输出量
Display ArrayList Elements
[10, 20, 30, 40, 50]
翻译自: https://www.includehelp.com/java/make-arraylist-read-only-in-java.aspx