使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

如何在Java中使ArrayList只读?相关推荐

  1. java arraylist 倒置_如何在Java中反转ArrayList?

    反转ArrayList 为了在Java中反转ArrayList的元素,我们使用Collections类的reverse()方法. reverse()方法reverse()方法在java.util包中可 ...

  2. 如何在Java中同步ArrayList?

    同步ArrayList (Synchronizing ArrayList) In java, there are two ways to synchronize ArrayList, 在Java中,有 ...

  3. java类只读怎么办_如何在Java中制作一个只读类?

    java类只读怎么办 The question is that "can we make a read-only class in Java?" 问题是"我们可以用Jav ...

  4. jpeg java_如何在java中使jpeg无损?

    JAI包提供了保存"无损JPEG"格式的能力.根据您想要的变体将压缩类型设置为 JPEG-LS or JPEG-LOSSLESS. 我不确定你真的想要无损JPEG.这是一种单独的格 ...

  5. java对arraylist排序吗_如何在Java中对ArrayList进行排序

    我有一堂水果课.我正在创建此类的列表,并将每个水果添加到列表中.我想根据水果名称的顺序对该列表进行排序. public class Fruit{ private String fruitName; p ...

  6. 如何在Java中处理ConcurrentModificationException? 在循环中从ArrayList中删除元素时要当心...

    从Java中从ArrayList中删除元素时常见的问题之一是ConcurrentModificationException. 如果您对索引使用经典的for循环或增强的for循环,并尝试使用remove ...

  7. Java中使用ArrayList的10个示例–教程

    Java中的ArrayList是HashMap之后最常用的集合类. Java ArrayList表示一个可自动调整大小的数组,并用于代替数组. 由于创建数组后我们无法修改数组的大小,因此我们更喜欢在J ...

  8. idea中java文件怎么运行_Java入门基础篇-如何在Java中创建只读文件

    本文选自千锋教育<Java语言程序设计>,如需转载请注明出处,谢谢! 1.如何创建只读文件 要使文件只读,我们只要将文件属性更改为只读就行:可以使用File类的setReadOnly()方 ...

  9. java整数的因式分解_如何在Java中找到整数的质数-因式分解

    java整数的因式分解 编程课程中的常见家庭作业/任务之一是关于Prime Factorization. 要求您编写一个程序以找到给定整数的素因子 . 一个数字的素数因子是将精确地除以给定数字的所有素 ...

最新文章

  1. python和java和scala_有没有与python3相当的scala/java收藏。国家
  2. leetcode 976. 三角形的最大周长
  3. DR.com客户端解密过程逆向分析
  4. 图像处理——图像增强
  5. Python返回数组(List)长度的方法
  6. ImageNet 数据集
  7. 为什么函数lamda显示权限不足_C++常用内置函数
  8. tcp压测工具_掌门全链路灰度压测实战
  9. (附源码)spring boot网上购物平台 毕业设计 141422
  10. 【智能制造】服装企业数字化转型之路
  11. 数据库分类及主流数据库对比
  12. matlab求一个矩阵的逆矩阵的命令,如何用MATLAB求逆矩阵
  13. 服务器双路cpu装什么系统,双路服务器CPU是什么意思?双路CPU是什么?
  14. informix mysql_Informix数据库查看数据库大小
  15. 服务器可不可以选择ssd硬盘
  16. 推特CEO继任者曝光:带着老婆孩子睡公司,钢铁侠20年嫡系,现在掌舵挖洞公司...
  17. 利用云信SDK实现前端实时聊天功能
  18. 小冲哥c语言视频自学网,C语言二级教学视屏课件_51自学网_小冲哥.doc
  19. IP更新、释放、清除DNS的详细操作方法
  20. APP微信支付(java后台_统一下单和回调)

热门文章

  1. POJ3984 迷宫问题【BFS】
  2. Being a Good Boy in Spring Festival【博弈】
  3. vue-cli 使用better-scroll
  4. Caffe CuDNN版本与环境不同导致make错误
  5. AdPlayBanner:功能丰富、一键式使用的图片轮播插件
  6. PLSQL_性能优化系列10_Oracle Array数据组优化
  7. disabling directory browsing
  8. asp.net文件上传进度条控件(破解版~没有时间限制) 多项自定义
  9. 计算机网络df例题,计算机网络期末试题北交.doc
  10. 基于Zookeeper使用ZkClient实现分布式锁