9

Googled for it, found plenty of code. But any of them gave me what I want. I want to make an ordinary array Immutable. I tried this:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class test {

    public static void main(String[] args) {

        final Integer array[];

        List<Integer> temp = new ArrayList<Integer>();
        temp.add(Integer.valueOf(0));
        temp.add(Integer.valueOf(2));
        temp.add(Integer.valueOf(3));
        temp.add(Integer.valueOf(4));
        List<Integer> immutable = Collections.unmodifiableList(temp);

        array = immutable.toArray(new Integer[immutable.size()]);

        for(int i=0; i<array.length; i++)
            System.out.println(array[i]);

        array[0] = 5;

        for(int i=0; i<array.length; i++)
            System.out.println(array[i]);

    }
}

But it doesnt work, I CAN assign a 5 into array[0] ... Is there any way to make this array immutable?

yak
  • 3,770
  • 19
  • 60
  • 111
  • 1
    http://stackoverflow.com/questions/3700971/immutable-array-in-java – Evgeni Dimitrov Jun 15 '13 at 11:12
  • 3
    Unlike C, you can't. However you can use a wrapper. – johnchen902 Jun 15 '13 at 11:12
  • 3
    Making you array variable final only prevents assigning a new array to it. As far as I know there no way to make it's values final. – André Stannek Jun 15 '13 at 11:12
  • why dont you use ArrayList or create your own wrapper? – ajay.patel Jun 15 '13 at 11:12
  • because my teacher said that this array cant be a collection :/ – yak Jun 15 '13 at 11:13
  • Well in that case you write your own wrapper, but let me tell you it would still be a collection:) . be it array or arraylist end of the day you use to for storing data. – ajay.patel Jun 15 '13 at 11:15
  • @yak your teacher has some fundamental misunderstandings about arrays, _or_ you did not tell us everything... But in any case SO is not a "homework solving" site – fge Jun 15 '13 at 11:17
  • 1
    @zerocool it may be a collection of elements, but not a `Collection` as defined by the JDK – fge Jun 15 '13 at 11:21
  • Agreed, but yak says that his teacher says that this Array Cant Be Collection, just wanted to clarify that even array is collection. I understand that its not same as java.util.Collection. – ajay.patel Jun 15 '13 at 11:25
  • So I guess I need to write this wrapper .. thanks!:) – yak Jun 15 '13 at 11:28
  • A small point of pedantry: An immutable list is not the same thing as an unmodifiable list. An immutable list *will not change*. An unmodifiable list is one that *you can't change through that list's regular access methods*. But `Collections.unmodifiableList` merely wraps the original list rather than creating a copy, so as long as you keep the original reference to `temp` around, you can call `temp.add` and watch the "immutable" list change during its lifetime. (On the other hand, `toArray` does create a copy, so even if the list really were immutable, the new array is always modifiable.) – Boann Jun 15 '13 at 11:44

3 Answers3

5

If you want to use it as an array, you can't.

You have to create a wrapper for it, so that you throw an exception on, say, .set(), but no amount of wrapping around will allow you to throw an exception on:

array[0] = somethingElse;

Of course, immutability of elements is another matter entirely!

NOTE: the standard exception to throw for unsupported operations is aptly named UnsupportedOperationException; as it is unchecked you don't need to declare it in your method's throws clause.

fge
  • 119,121
  • 33
  • 254
  • 329
3

It's impossible with primitive arrays.

You will have to use the Collections.unmodifiableList() as you already did in your code.

darijan
  • 9,725
  • 25
  • 38
1

You can also use Guava's ImmutableList, a high-performance, immutable, random-access List implementation that does not permit null elements. Unlike Collections.unmodifiableList(java.util.List), which is a view of a separate collection that can still change, an instance of ImmutableList contains its own private data and will never change.

1218985
  • 7,531
  • 2
  • 25
  • 31
  • However, OP says the "end result" must not be a `Collection`... Otherwise, yeah, Guava's `Immutable*` rock. – fge Jun 15 '13 at 11:42