3

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!

0

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks Liv for your answer...Basically I am trying to implement ArrayList using array.So as in Collections we have this method unmodifiableList, how can I implement the same ArrayList which I have implemented using array as readonly. I hope I am able to make the question clear now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.