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!

Community
  • 1
  • 1
Biswajit
  • 31
  • 1
  • 2

1 Answers1

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.

Liv
  • 6,006
  • 1
  • 22
  • 29
  • 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. – Biswajit Apr 27 '11 at 16:19