Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an array in Java read only? [duplicate]

Tags:

java

arrays

Possible Duplicates:
Java unmodifiable array
Immutable array in Java

How do I make an array read only so that the elements inside it can only be read but cannot be modified,added or deleted. need to do this in JAVA. Please help. I think merely the use of final keyword wont help.Need to do something more than that at the code level. Thanks in advance!

like image 275
Biswajit Avatar asked Mar 21 '26 13:03

Biswajit


1 Answers

Short answer is you can't -- final will only guarantee you that the reference to the array itself won't be changed. You can do this with a List though, as the Collections class provides a method for creating a List that cannot be modified (Collections.unmodifiableList) -- that is only if you can change your application to use List rather than array.

like image 123
Liv Avatar answered Mar 23 '26 05:03

Liv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!