日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

如何在Java中使ArrayList只读?

發布時間:2025/3/11 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在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只读?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。