Re: Make Array Unmodifiable?

Discussion in 'Java' started by Rune Berge, May 31, 2004.

  1. Rune Berge

    Rune Berge Guest

    Ken wrote:
    > Is there a standard approach or pattern for making an array that
    > cannot be modified? I ask this in this context: I'd like to be able
    > to return an array from a class method without letting the caller
    > subsequently modify the array. In C++ I'd return it as a const. Java
    > does not have such a construct. It seems that collections can handle
    > something like this, but then I lose the type safety of arrays.
    >
    > Any suggestions?


    Return a copy of the array. Then it doesn't matter if it is modified. Or
    use the type safe collections in the 1.5 beta.

    Rune
     
    Rune Berge, May 31, 2004
    #1
    1. Advertising

  2. Rune Berge

    Roedy Green Guest

    On Mon, 31 May 2004 15:06:40 +0200, Rune Berge <>
    wrote or quoted :

    >Return a copy of the array. Then it doesn't matter if it is modified. Or
    >use the type safe collections in the 1.5 beta.



    You mean pass a clone of the array to the method. He does not trust
    the callee not to modify the array.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 31, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. P.Hill

    Re: Make Array Unmodifiable?

    P.Hill, May 31, 2004, in forum: Java
    Replies:
    1
    Views:
    3,642
    Andrew Thompson
    May 31, 2004
  2. Roedy Green

    Re: Make Array Unmodifiable?

    Roedy Green, May 31, 2004, in forum: Java
    Replies:
    24
    Views:
    967
    Tony Morris
    Jun 3, 2004
  3. Laax
    Replies:
    2
    Views:
    453
  4. Ian Pilcher

    Unmodifiable ResultSet wrapper?

    Ian Pilcher, Aug 29, 2011, in forum: Java
    Replies:
    3
    Views:
    741
  5. LAMBEAU Bernard
    Replies:
    1
    Views:
    120
    Markus Schirp
    Jan 8, 2009
Loading...

Share This Page