
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to make elements of array immutable in Java?
No, you cannot make the elements of an array immutable.
But the unmodifiableList() method of the java.util.Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object. The user has only read-only access to the obtained list.
And the asList() method of the ArrayList class accepts an array and returns a List object.
Therefore, to convert an array immutable −
Obtain the desired array.
Convert it into a list object using the asList() method.
Pass the obtained list as a parameter to the unmodifiableList() method.
Example
import java.util.Arrays; import java.util.Collections; import java.util.List; public class UnmodifiableExample { public static void main(String args[]) { //Creating a string array String strArray[] = {"Raju", "Rama", "Rahman", "Rachel", "Ranbhir", "Rangan"}; //Converting the string array to list object List<String> list = Arrays.asList(strArray); //Converting the List object to immutable List<String> immutable = Collections.unmodifiableList(list); System.out.println(immutable); immutable.add("komala"); } }
Output
[Raju, Rama, Rahman, Rachel, Ranbhir, Rangan] Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Unknown Source) at September19.UnmodifiableExample.main(UnmodifiableExample.java:19)
- Related Articles
- How to make object properties immutable in TypeScript?
- How to create immutable class in Java?
- Golang program to make string immutable
- How to create an immutable class in Java?
- How to create the immutable class in Java?
- How to create an immutable set in Java?
- How to initialize immutable collections in Java 9?
- How to remove duplicate elements of an array in java?
- Decrease Elements To Make Array Zigzag in Python
- How to Alter Two Array Elements in Java
- Immutable String in Java
- How to add elements to the midpoint of an array in Java?
- How to sort Java array elements in ascending order?
- How to use JOptionPane with Array Elements in Java?
- How to Find Single Digit Array Elements in Java?

Advertisements