如何在Java中使ArrayList只读?
使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 classimport java.util.*;public class ArrayListMakeReadOnly {public static void main(String[] args) {// ArrayList DeclarationCollection arr_list = new ArrayList();// By using add() method to add few elements in // ArrayListarr_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-OnlyCollection 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
總結
以上是生活随笔為你收集整理的如何在Java中使ArrayList只读?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ssm中java实现树状结构_java
- 下一篇: C ++中带有示例的llabs()函数